检查字符串中重复的字符/字母 [英] check for repeated characters/letter in a string
本文介绍了检查字符串中重复的字符/字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi我想检查一个字符串从用户输入在一个字符串中有两个重复字符/字母eachother ..我有一个简单的代码,以检查字符串中的第一个字母和第二个字母是否相同。 / p>
hi i want to check if a string from user input has two repeated characters/letters in a string next to eachother.. i have a simple code to check if the first letter and second letter in the string are the same.
def two_same():
d = []
while len(d) <= 1:
d = input("enter the same letter twice:")
if d[0] == d[1]:
return print(d[0])
two_same()
但是如何检查字符串中的所有字符和用户输入的重复字母。
but how would i check all the characters in the string and for a repeated letter from a user input.
推荐答案
正如已经提到的意见,你应该使用一个for循环:
As the comments already mentioned you should use a for loop:
def two_same(string)
for i in range(len(string)-1):
if string[i] == string[i+1]:
return True
return False
result = ""
while not two_same(result):
result = input("enter the same letter twice:")
print(result)
这篇关于检查字符串中重复的字符/字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文