Python中的字符串代码

img


import re
str = "pear's unit price is 9 yuan" 
f = re.match('^pear.*(\d+).*yuan$',str)
print(f.group(1))
print(type(f))
sentence = "pear's unit price is 9 yuan"
num = sentence[21]  # 21索引的对应值即为9
print(type(num))        # type(obj)可以返回obj的类型

"""
=====运行结果=====
<class 'str'>
"""