Instagram
youtube
Facebook
Twitter

Remove Repeated Characters from a String

A Python program to remove duplicate characters from a string while preserving the order of first occurrences.

Here's a explanation of code in points:

  • def repeated_character_remove(string):
    ➤ Defines a function to remove repeated characters from the input string.

     
  • new_string = ""
    ➤ Initializes an empty string to store unique characters.

     
  • for char in string:
    ➤ Loops through each character in the input string.

     
  • if char not in new_string:
    ➤ Check if the character is not already in new_string.

     
  • new_string += char
    ➤ If it's a new character, add it to new_string.

     
  • return new_string
    ➤ Returns the string with only unique characters.

     
  • string = input("Enter a string: ")
    ➤ Takes input from the user.

     
  • result = repeated_character_remove(string)
    ➤ Calls the function and stores the result.

     
  • print("Unique characters:", result)
    ➤ Prints the final string without duplicates.

 

Program:

def repeated_character_remove(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 = repeated_character_remove(string)

print("Unique characters:", result)