首页 > 解决方案 > 删除python字符串中数字1之前的数字

问题描述

我想知道是否可以在字符串中的数字 1 之前删除数字:

original_string = 'Word 2 def 1 here 2 what 3 for 4 that'

desired_string = 'Word def 1 here 2 what 3 for 4 that'

标签: pythonstring

解决方案


您也可以使用它:

import re

original_string = 'Word 2 def 4 1 here 2 what 3 for 4 that'
if '1' in original_string:
     indexfirst=int(original_string.index('1')) 
else:
     indexfirst=0

original_string=re.sub('[023456789]', '', original_string[0:indexfirst])+original_string[indexfirst:]
print(original_string)

推荐阅读