Sinoda Sinoda - 3 years ago 249
Python Question

check for repeated characters/letter in a string

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.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download