如何检查字符串中的特定字符?

编程入门 行业动态 更新时间:2024-10-11 21:24:40
本文介绍了如何检查字符串中的特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用 Python 2 检查字符串中是否包含多个特定字符?

How can I check if a string has several specific characters in it using Python 2?

例如,给定以下字符串:

For example, given the following string:

犯罪分子偷走了价值 1,000,000 美元的珠宝.

The criminals stole $1,000,000 in jewels.

如何检测它是否有美元符号($")、逗号(,")和数字?

How do I detect if it has dollar signs ("$"), commas (","), and numbers?

推荐答案

假设你的字符串是 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 = repile(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')

更多推荐

如何检查字符串中的特定字符?

本文发布于:2023-11-12 13:33:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!