+-

如何使用 Python2检查字符串值是否包含确切的字符?
具体来说,我希望检测它是否有美元符号(“$”),逗号(“,”)和数字.
具体来说,我希望检测它是否有美元符号(“$”),逗号(“,”)和数字.
最佳答案
假设你的字符串是s:
'$' in s # found
'$' not in s # not found
# original answer given, but less Pythonic than the above...
s.find('$')==-1 # not found
s.find('$')!=-1 # found
等等其他角色.
… 要么
pattern = re.compile(r'\d\$,')
if pattern.findall(s):
print('Found')
else
print('Not found')
… 要么
chars = set('0123456789$,')
if any((c in chars) for c in s):
print('Found')
else:
print('Not Found')
[编辑:在答案中添加’$’]
点击查看更多相关文章
转载注明原文:如何检查字符串中的特定字符? - 乐贴网