Use enumerate function, for loop and if statement to find the first repeated character in a given string.
Python program to find the first repeated character in a given string Examples
Simple example code finds the character which occurs more than once. If there is no repeating character “None“.
def rep_char(str1):
s = str1.lower()
for index, c in enumerate(s):
if s[:index + 1].count(c) > 1:
return c
return "None"
print(rep_char("Python"))
print(rep_char("EyeHunts"))
Output:
![Python Find the first repeated character in a given string](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2021/08/Python-Find-the-first-repeated-character-in-a-given-string.jpg?resize=237%2C145&ssl=1)
Do comment if you have any doubts and suggestions on this Python char program.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.