Python program to find all non repeating characters in the string

In this tutorial, we will learn writing the python program to print all characters of the string which are not repeating. We can say the character are available only one time in string.

Problem Statement

Take any string as an input from the user and check if any character in the string is repeating or not. if it is not repeating then print that character as output.

For example:

Case1: If the user inputs the string ‘pythonprogramming’

Then the output should be ‘ythai’, where there is no character that is repeating.

Case2: If the user inputs the string ‘quescoll’

Then the output should be ‘quesco’, where there is no character that is repeating.

Our logic to find all non repeating characters in the string

  • Our program will take a string as an input from the user.
  • Iterate the character of the input string to check if the character is repeating or not, to achieve this we use the concept of ‘nested loop’.
  • The output is in the form of a string, where all the non-repeating characters are concatenated together using concatenation methods.

Algorithm to find all non repeating characters in the string

Step1: Start
Step2: Take a string as an input from the user
Step3: Create an empty string result=”” to store non-repeating characters in the string.
Step4: iterate through each character of the string
Step5: Declare a variable count=0 to count appearance of each character of the string
Step6: for j in string:
if i==j:
increment count to 1
if count is greater than one:
break the current iteration of nested loop
if count is equal to 1:
concatenate the character with the empty string, result
Step7: print result as the output of our program
Step8: Stop

Python code to find all nonrepeating characters in the String

string = input('Please enter a string: ')
freq_dict = {}
for char in string:
    if char in freq_dict:
        freq_dict[char] += 1
    else:
        freq_dict[char] = 1
non_repeating_chars = ""
for char in string:
    if freq_dict[char] == 1:
        non_repeating_chars += char
print("Non-repeating characters:", non_repeating_chars)

Output:

Please enter a string: hello quescol
Non-repeating characters: h qusc

Explanation:

For the input string ‘hello quescol’, as the characters ‘e’, ‘l’,’o’ are repeating but ‘h’, ‘q’, ‘s’, and ‘c’ are not repeating so the generated output should be ‘h qusc’.