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

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.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.