提问人:NND 提问时间:11/13/2023 更新时间:11/14/2023 访问量:61
查询来自 python。我有一个句子的输入。我想把这句话分成每个字母表。然后我想使用以下功能。建议
Query is from python. i have an input of sentence. I want to split the sentence into each alphabet. Then I want to use following function. Suggest
问:
在下面的代码中,当我键入 Sam 作为输入时,它显示正确的结果为“字符串包含字母 s” 但是当我键入 Sam Aron 时,它首先显示 Sam 的两条消息,因为 String 包含字母 s,然后显示 String 不包含单词 Aron 的字母 s,在给定输入中由空格分隔。 我怎样才能使它把完整的输入看作是单个字符串,并为完整的句子提供单个消息,例如字符串包含字母S(对于'Sam Aron')
def check_string():
a = input()
b = a.split()
for i in b:
if 's' in i or 'S' in i:
print('String contains letter s')
else:
print('String does not contain letter s')
check_string()
答:
-1赞
Ahmed Ashraf
11/13/2023
#1
如果要检查整个字符串中的字母“S”,请不要将输入分成空格。
def check_string():
a = input()
for i in a:
if 's' == i or 'S' == i:
print('String contains letter s')
else:
print('String does not contain letter s')
check_string()
但是,有更好的方法可以做到这一点
def check_string():
a = input().lower()
if 's' in a:
print('String contains letter s')
else:
print('String does not contain letter s')
评论
1赞
Guy
11/13/2023
你试过第一个代码吗?
0赞
Ahmed Ashraf
11/14/2023
@Guy我错过了第一个代码片段中的平等部分:)然而,第二个是实际的解决方案。无论如何,编辑
0赞
NND
11/14/2023
谢谢@AhmedAshraf,更好的工作方式
评论
b = a.split()
if 's' in a.lower()