How to find repeated numbers in a string in python

return is a keyword that works more or less as immediately exit this function (and optionally carry some output with you). You thus need to remove the return statement:

def find_duplicate():
    x =input("Enter a word = ")
    for char in x :
        counts=x.count(char)
        print(char,counts)

Furthermore you also have to remove the while loop (or update the counter if you want to print multiple times), otherwise you will get stuck in an infinite loop since count is not updated and the test will thus always succeed.

Mind however that in this case a will be printed multiple times (in this case two) if it is found multiple times in the string. You can solve this issue by first constructing a set of the characters in the string and iterate over this set:

def find_duplicate():
    x =input("Enter a word = ")
    for char in set(x):
        counts=x.count(char)
        print(char,counts)

Finally it is better to make a separation between functions that calculate and functions that do I/O (for instance print). So you better make a function that returns a dictionary with the counts, and one that prints that dictionary. You can generate a dictionary like:

def find_duplicate(x):
    result = {}
    for char in set(x):
        result[char]=x.count(char)
    return result

And a calling function:

def do_find_duplicates(x):
    x =input("Enter a word = ")
    for key,val in find_duplicate(x).items():
        print(key,val)

And now the best part is: you actually do not need to write the find_duplicate function: there is a utility class for that: Counter:

from collections import Counter

def do_find_duplicates(x):
    x =input("Enter a word = ")
    for key,val in Counter(x).items():
        print(key,val)

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string, find all the duplicate characters which are similar to each other. Let us look at the example. 

    Examples:

    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s

    We have discussed a solution in the below post. Print all the duplicates in the input string We can solve this problem quickly using the python Counter() method. 

    The approach is very simple. 

    1. Create a dictionary using the Counter method having strings as keys and their frequencies as values.
    2. Declare a temp variable.
    3. Print all the indexes from the keys which have values greater than 1. 

    Python

    from collections import Counter

    def find_dup_char(input):

        WC = Counter(input)

        for letter, count in WC.items():

            if (count > 1):

                print(letter)

    if __name__ == "__main__":

        input = 'geeksforgeeks'

        find_dup_char(input)

    Approach : Using count() method

    Python3

    def find_dup_char(input):

        x=[]

        for i in input:

            if i not in x and input.count(i)>1:

                x.append(i)

        print(" ".join(x))

    if __name__ == "__main__":

        input = 'geeksforgeeks'

        find_dup_char(input)


    How do I find a repeating element in a string Python?

    Python.
    string = "Great responsibility";.
    print("Duplicate characters in a given string: ");.
    #Counts each character present in the string..
    for i in range(0, len(string)):.
    count = 1;.
    for j in range(i+1, len(string)):.
    if(string[i] == string[j] and string[i] != ' '):.
    count = count + 1;.

    How do you check for duplicate numbers in Python?

    ALGORITHM:.
    STEP 1: Declare and initialize an array..
    STEP 2: Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. ... .
    STEP 3: If a match is found which means the duplicate element is found then, display the element..

    How do you find the repetition of a string?

    Approach:.
    Find the occurrences of character 'a' in the given string..
    Find the No. of repetitions which are required to find the 'a' occurrences..
    Multiply the single string occurrences to the No. ... .
    If given n is not the multiple of given string size then we will find the 'a' occurrences in the remaining substring..

    How do you find the number of repeated values in a list in Python?

    Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.