How to get Unique Characters in a String in Python?

Python – Unique Character in a String

To get unique characters in a given String in Python, pass the string to set() method. Since, String is an iterable of characters, set() method creates a Set of characters. And since Set holds only unique items, set() returns unique characters present in the given string.

Program

In the following program, we read a string from user using input() function, and then find the unique characters in this string.

Python Program

x = input()
unique_characters = set(x)
print(unique_characters)

Output #1

banana 
{'n', 'a', 'b'}

Output #2

aaaabaaacccccabbbbddeeeee
{'c', 'a', 'e', 'b', 'd'}

References

Summary

In this Python Tutorial, we learned how to find unique characters present in a given string.

Privacy Policy Terms of Use

SitemapContact Us