对于上/下ch的任意组合,如何检查字符串a是否包含在较长的字符串B中

2024-10-01 07:27:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中寻找一个函数,用于检查字符串a是否包含在字符串B中,以及两个字符串的上/下字符的任意组合。你知道吗

示例:

a = 'uaUa'
b = 'this is a longer string containing uaua'

checkString (a, b)返回True,因为a包含在b中


Tags: 函数字符串true示例stringisthis字符
3条回答

试试https://docs.python.org/2/library/re.html#re.search

>>> import re
>>> a = 'uaUa' 
>>> b = 'this is a longer string containing uaua'
>>> print bool( re.search(a, b, re.IGNORECASE) )
True

您可以使用“.lower()”方法将两个字符串转换为小写(例如),然后使用Python字符串功能的标准find方法。你知道吗

def checkString(a, b):
    return a.lower() in b.lower()

相关问题 更多 >