Instagram
youtube
Facebook
Twitter

Python Program to Print All Non-Repeating Characters in a String

A Python program that prints all non-repeating characters from a string while preserving their original order.

Code Explanation in Points :

def unique_characters(string):

  • A function is defined to return all non-repeating characters in the order they appear.
     

new_string = ""

  • A new empty string is created to store unique characters.
     

for char in string:

  • Loop goes through each character in the input string.
     

if char not in new_string:

  • Checks if the character is already in new_string.
     
  • If not, it's added (only the first occurrence is kept).
     

return new_string

  • Returns the string containing only the first occurrence of each character.
     

Input and Output part:

  • Takes user input using input().
     
  • Call the function with the input string.
     
  • Prints the result.

 

program:

def unique_characters(string):

    new_string = ""

    for char in string:

        if char not in new_string:

            new_string += char

    return new_string

string = input("Enter a string: ")

result = unique_characters(string)

print("Unique characters in the string:", result)