Python 字符串分割:按数字划分
可以使用正则表达式来匹配字符串中的数字,并通过 re.split() 函数来划分字符串。
以下是示例代码:
import re
s = 'hello123world456python789'
# 匹配数字
pattern = re.compile(r'\d+')
# 划分字符串
result = pattern.split(s)
print(result)
输出:
['hello', '123', 'world', '456', 'python', '789']
这样就将字符串按照数字进行了划分。
原文地址: https://www.cveoy.top/t/topic/of9P 著作权归作者所有。请勿转载和采集!