Write a Python program to remove duplicate characters of a given string


The program is written in Python and it performs the following operations:

  • The first line imports the "OrderedDict" class from the "collections" module. The "OrderedDict" is a subclass of "dict" that maintains the order of elements in the dictionary.
  • In the second line, a string "String and String Function" is assigned to the variable "str".
  • In the third line, the "join" method is called on an empty string, and the argument passed to it is the result of calling the "fromkeys" method on the "OrderedDict" class. The "fromkeys" method creates a new ordered dictionary with elements from the input string as keys and all of their values are set to None.
  • By using the "join" method on an empty string, the program concatenates all of the keys in the ordered dictionary and returns a new string with all of the duplicate characters removed.
  • The "print" function is used to print the result of the operation, which is the string with all of the duplicate characters removed.

In summary, this program demonstrates how to remove duplicates from a string by using the "OrderedDict" and "join" methods.

Source Code

from collections import OrderedDict
str = "String and String Function"
print("".join(OrderedDict.fromkeys(str)))

Output

String adFuco

Example Programs