search()和match(),用起来更灵活
import re
str = "Welcome to my world. I have 12 apples."
if re.search(r"world", str).group() != "" :
print("match! ")
str = "abcABC"
if re.match(r"[a-zA-Z]+", str):
print("match! ", re.search(r"[A-Z]+", str).group())
else:
print("ummatch! ")
字符串“ 我们是中国人 我爱我 的祖国 ”
1、查找空格数量
2、删除前后空格
3、删除所有空格
4、找到“祖国”的位置
5、判断字符串是否以“我们”开 头
str01=" 我们是中国人 我爱我 的祖国 "
print(str01.count(" "))#查找空格个数
print(str01.lstrip())#删除开头空格
print(str01.rstrip())#删除末尾空格
print(str01.replace(" ",""))#删除所有空格
print(str01.find("祖国"))
print(str01.find("我们",0,1))