相当于 contain 的 unicode 字符串
unicode string equivalent of contain
尝试在 python 中使用包含时出现错误。
s = u"some utf8 words"
k = u"one utf8 word"
if s.contains(k):
print "contains"
如何获得相同的结果?
带有普通 ASCII 字符串的示例
s = "haha i am going home"
k = "haha"
if s.contains(k):
print "contains"
我正在使用python 2.7.x
str
和unicode
没有区别。
print u"ábc" in u"some ábc"
print "abc" in "some abc"
基本相同
ascii 和 utf8 字符串相同:
if k in s:
print "contains"
ascii 或 uft8 字符串上都没有 contains()
:
>>> "strrtinggg".contains
AttributeError: 'str' object has no attribute 'contains'
您可以使用 find
或 index
:
代替 contains
if k.find(s) > -1:
print "contains"
或
try:
k.index(s)
except ValueError:
pass # ValueError: substring not found
else:
print "contains"
当然,in
运算符是正确的选择,它更优雅。
字符串没有 "contain" 属性。
s = "haha i am going home"
s_new = s.split(' ')
k = "haha"
if k in s_new:
print "contains"
我猜你想实现这个
测试字符串是否存在于字符串中
string = "Little bear likes beer"
if "beer" in string:
print("Little bear likes beer")
else:
print("Little bear is driving")
尝试在 python 中使用包含时出现错误。
s = u"some utf8 words"
k = u"one utf8 word"
if s.contains(k):
print "contains"
如何获得相同的结果?
带有普通 ASCII 字符串的示例
s = "haha i am going home"
k = "haha"
if s.contains(k):
print "contains"
我正在使用python 2.7.x
str
和unicode
没有区别。
print u"ábc" in u"some ábc"
print "abc" in "some abc"
基本相同
ascii 和 utf8 字符串相同:
if k in s:
print "contains"
ascii 或 uft8 字符串上都没有 contains()
:
>>> "strrtinggg".contains
AttributeError: 'str' object has no attribute 'contains'
您可以使用 find
或 index
:
contains
if k.find(s) > -1:
print "contains"
或
try:
k.index(s)
except ValueError:
pass # ValueError: substring not found
else:
print "contains"
当然,in
运算符是正确的选择,它更优雅。
字符串没有 "contain" 属性。
s = "haha i am going home"
s_new = s.split(' ')
k = "haha"
if k in s_new:
print "contains"
我猜你想实现这个
测试字符串是否存在于字符串中
string = "Little bear likes beer"
if "beer" in string:
print("Little bear likes beer")
else:
print("Little bear is driving")