Print Unique Characters of a String
To print unique characters of a string in Python, iterate over the characters of the string, prepare a list of unique characters, and print them.
Example
In the following program, we take a string in name
variable, and print the unique characters in this string to console.
main.py
</>
Copy
myString = 'banana'
uniqueChars = []
#get unique characters into a list
for ch in myString :
if ch not in uniqueChars :
uniqueChars.append(ch)
#print unique characters list
for ch in uniqueChars :
print(ch)
Output
b
a
n
Reference
In the above program(s), we have used the following Python concepts. The links have been provided for reference.
- For Loop
- List
- Create empty list in Python
- Add item to list
- Python not logical operator
- Python in – membership operator
- Python print() builtin function
Conclusion
In this Python Tutorial, we learned how to print unique characters of a given string in Python.