字符串中存在特定字符

Presence of a particular character in a string

如何检查一个字符串是否至少有一个特定字符?

如果字符串 cool = "Sam!",我如何检查该字符串是否至少有一个 !

使用 in 运算符

>>> cool = "Sam!"
>>> '!' in cool
True
>>> '?' in cool
False

如您所见 '!' in cool returns 一个布尔值,可以在您的代码中进一步使用

使用以下内容

cool="Sam!"
if "!" in cool:
    pass # your code

或者只是:

It_Is="!" in cool
# some code
if It_Is:
    DoSmth()
else:
    DoNotDoSmth()

在Python中,字符串是一个序列(类似于数组);因此,in 运算符可用于检查 Python 字符串中是否存在字符。 in 运算符用于断言序列中的成员资格,例如字符串、列表和元组。

cool = "Sam!"
'!' in cool # True

或者,您可以使用以下任一方式获取更多信息:

cool.find("!") # Returns index of "!" which is 3
cool.index("!") # Same as find() but throws exception if not found
cool.count("!") # Returns number of instances of "!" in cool which is 1

可能对您有帮助的更多信息:

http://www.tutorialspoint.com/python/python_strings.htm