小册名称:Python合辑4-130个字符串操作示例
在 Python 中检查字符串是否以 XXXX 开头
import reexp_str = "Python Programming"# Example 1if re.match(r'^Python', exp_str): print(True)else: print(False)# Example 2result = exp_str.startswith("Python")print(result)
import re
exp_str = "Python Programming"
# Example 1
if re.match(r'^Python', exp_str):
print(True)
else:
print(False)
# Example 2
result = exp_str.startswith("Python")
print(result)
Output:
TrueTrue
True