在python中如何查找字符串用单引号引起来
arr=["string","string'1" ,"'string2'"]
for i in arr:
if i //string enclosed in single quotes condition
print "found"
print i //which sould print only string2 in this case
最简单的方法是检查它是否都以引号开头和结尾:
if i.startswith("'") and i.endswith("'"):
print "found"
当然,这不会告诉您任何其他信息,例如它是否包含匹配的引号,或者实际上包含的不只是单个引号字符。如果那很重要,则添加一个检查字符串是否大于一个字符(如果希望在引号之间包含一些字符,则大于2):
if i.startswith("'") and i.endswith("'") and len(i) > 1:
print "found"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句