Write a python program to find the first repeated character in a given string

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

Do comment if you have any doubts and suggestions on this Python char program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.