Extract Number before a Character in a String Using Python

前端 未结 2 1875
梦如初夏
梦如初夏 2020-12-11 07:35

I\'m trying to extract the number before character \"M\" in a series of strings. The strings may look like:

\"107S33M15H\"
\"33M100S\"
\"12M100H33M\"
         


        
2条回答
  •  一生所求
    2020-12-11 08:02

    You can use rpartition to achieve that job.

    s = '107S33M15H'    
    prefix = s.rpartition('M')[0]
    

提交回复
热议问题