我试图返回“是”或“否”的答案,以显示字符串是否有空格或字母。黑体部分是我需要纠正的部分。我还有其他部分..。
这是我放的东西,结果不正确.
if (string.isalpha() and string.isspace()):
print('Only alphabetic letters and spaces: yes')
else:
print('Only alphabetic letters and spaces: no')我怎么才能把它们放在一起?我知道如何分开做,但不能一起做。我知道这看起来很简单,但我是个初学者。
程序的例子
输入字符串:请打开我的电子邮件
长度: 20
第一个字符:P
最后一个角色:我
包含打开:是的
只使用字母和空格:是的,
只有数字数字:否
全小写:是
全大写:否
发布于 2015-04-02 04:08:09
你在测试整根绳子。你应该单独测试每个字符。
is_alpha_or_space = all(c.isalpha() or c.isspace() for c in string)发布于 2015-04-02 04:07:54
您可以使用or和all检查每个字母
if all(char.isalpha() or char.isspace() for char in string):
print('Only alphabetic letters and spaces: yes')发布于 2015-04-02 04:12:37
有两种选择:
if all(x.isalpha() or x.isspace() for x in original):或
original.replace(' ','').isalpha()应该行得通。
https://stackoverflow.com/questions/29404785
复制相似问题