一个字符串的末尾可以同时带有单'_'
和双'__'
下划线字符。
检查它:
if myString[-1]=='_': pass
仅适用于单个字符。如何使用一条线实现相同的目标?
(仅当最后一个字符 =='_' 或 '__' 时)。
最佳答案
对于 Python 版本 > 2.5,
您可以使用 endswith
with a tuple
underscores = ('_', '__')
if myString.endswith(underscores): pass
演示
>>> underscores = ('_', '__')
>>> xx = "text__"
>>> xxx = "text_"
>>> xx.endswith(underscores)
True
>>> xxx.endswith(underscores)
True
>>>
关于Python:如何在一行中检查字符串中出现的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178417/