seq = "q3option54"
seq_rev = seq[::-1] # reverse the sequence
rev_ind = 0
# find the last sequence of numbers
for ind, char in enumerate(seq_rev):
if not char.isdigit():
rev_ind = ind
break
# slice off the digit part, and reverse it
digits = seq_rev[:rev_ind][::-1]
print(f'digits: {digits}')
您可以为此使用正则表达式:
Outuput
如果您知道所有的数字都在字符串的末尾,在一个不间断的序列中,那么从字符串的末尾开始搜索第一个字母
输出:
相关问题 更多 >
编程相关推荐