# -*- coding:UTF-8 -*-
"""
字符串中查找子串的方法
检查字符串s2中是不是含有s1
"""
# 方法1:
def is_sub_string1(s1, s2):
tag = False
len1 = len(s1)
len2 = len(s2)
for i in range(0, len2):
if s2[i] == s1[0]:
for j in range(0, len1):
if s2[i] == s1[j]:
tag = True
return tag
# 方法2:利用字符串自带的find()方法
def is_sub_string2(s1, s2):
tag = False
if s2.find(s1) != -1:
tag = True
return tag
# 方法3:关键字"in”不仅可以用于列表、元祖等数据类型,还可以用于字符串。
# 所以,这里只需要直接一行代码搞定:
def is_sub_string3(s1, s2):
return s1 in s2
python 字符串中查找子串
最新推荐文章于 2024-06-13 00:31:58 发布