Skip to content

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

  • by

Use OrderedDict from collections module and join method to remove duplicate characters of a given string in Python.

Python program to remove duplicate characters of a given string Example

Simple example code.

from collections import OrderedDict


def rem_dup(str1):
    # Lower case if needed
    # str1 = str1.lower()
    return "".join(OrderedDict.fromkeys(str1))


print(rem_dup("ABC ABC EFGH EFGH"))
print(rem_dup("ABC abc"))

Output:

Python program to remove duplicate characters of a given string

Do comment if you have any doubts and suggestions on this Python char string program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *