Find repeated characters in python

Python repeated character in string

Given a string of characters. Write python code to print the repeated characters


This is a typical dictionary based problem. Here is how to do that in Python.

# Input string
str = "Computers are useless. They can only give you answers"

# Initialize dictionary
dic = {}
for char in str:
   dic[char] = 0

# Update char counts in the dictionary
for char in str:
   # char is used as the key
   # and the value is the count
   # of that char in the string
   # Repeated chars hash to the
   # same position
   dic[char]+= 1

# Print characters with count > 1
for char, count in dic.items():
   if count > 1 and char != ' ':
      print "%c is repeated %d times" % (char, count)

You may need to take a look at the following similar articles

Thanks for visiting. For feedback, please use the comments section below.

Search Terms...

Leave a Reply