# 示例代码:使用 Python 中的 find 函数
# 定义一个字符串
text = "Hello, welcome to the world of Python!"
# 使用 find 函数查找子字符串 "Python" 的位置
position = text.find("Python")
# 输出结果
print(f"'Python' 在字符串中的位置是: {position}")
# 如果子字符串不存在,则返回 -1
position_not_found = text.find("Java")
print(f"'Java' 在字符串中的位置是: {position_not_found}")
find()
函数用于查找子字符串在字符串中的位置。如果找到,返回子字符串的第一个字符的索引;如果没有找到,则返回 -1
。text.find("Python")
返回的是子字符串 "Python" 在 text
中首次出现的位置,而 text.find("Java")
由于 "Java" 不在 text
中,因此返回 -1
。上一篇:判断闰年python
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站