March 2, 2017
Find repeated characters in python
Python repeated character in string
Given a string of characters. Write python code to print the repeated characters
Solution
This is a typical dictionary based problem. Here is how to do that in Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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
- Character count in string java
- String repeated characters count
- Most repeated character
- Non-repeating character string
- First non-repeating character hash
- Sql unique distinct select
Thanks for visiting. For feedback, please use the comments section below.
More from my site
2 Comments
Write a program to count repeated characters in a given string.
Input Format
todetermineifaspecifiedkeyispresentinadictionaryusetheinkeyword
Constraints
Upper character and lower character should be considered seperately
Output Format
t 5, o 3, d 4, e 11, r 4, m 1, i 9, n 5, f 2, a 3, s 4, p 2, c 2, k 2, y 3, u 1, h 1, w 1
Sample Input 0
programming
Sample Output 0
p 1, r 2, o 1, g 2, a 1, m 2, i 1, n 1
Hi sir.., I need Python Program for this below String….
Input : aabcdeefggg
Output : gea
Count: 7