# 示例代码:检查字符串是否以指定字符或子字符串开头
# 使用 str.startswith() 方法
text = "hello world"
prefix = "hello"
# 检查字符串是否以 'hello' 开头
if text.startswith(prefix):
print(f"'{text}' 以 '{prefix}' 开头")
else:
print(f"'{text}' 不以 '{prefix}' 开头")
# 输出: 'hello world' 以 'hello' 开头
str.startswith()
是 Python 内置的字符串方法,用于检查字符串是否以指定的字符或子字符串开头。True
,否则返回 False
。text
是 "hello world"
,而 prefix
是 "hello"
。由于 text
确实以 "hello"
开头,因此输出结果为 'hello world' 以 'hello' 开头
。上一篇:python3 字典
下一篇:python 匹配字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站