Skip to content

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

  • by

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.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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading