• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Extract Unique Characters From String in Python

Extract Unique Characters From String in Python

Author: Aditya Raj
Last Updated: January 2, 2023

Strings are extensively used for text analysis in Python. This article discusses how to extract unique characters from a string in Python.

Table of Contents
  1. Extract Unique Characters From String in Python Using for Loop
  2. Extract Unique Characters From String in Python Using Sets
  3. Unique Characters From String in Python Using Counter() Method
  4. Conclusion

Extract Unique Characters From String in Python Using for Loop

To extract unique characters from a string using for loop, we will first define an empty list to store the output characters. Then, we will iterate through the string characters using a for loop. While iteration, we will check if the current character is present in the list or not. If yes, we will move to the next character. Otherwise, we will add the current character to the list using the append() method. The append() method, when invoked on a list, takes the character as its input argument and appends it to the end of the list.

After execution of the for loop, we will get all the unique characters of the string in our list. You can observe this in the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=[]
for character in myStr:
    if character not in output:
        output.append(character)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: ['P', 'y', 't', 'h', 'o', 'n', ' ', 'F', 'r', 'B', 'e', 'g', 'i', 's']

In the above example, we have used the string “Python For Beginners”. After execution of the for loop, we get the list of unique characters from the string.

Extract Unique Characters From String in Python Using Sets

Python set data structure is used to store unique immutable objects together. As python strings as well as individual characters are immutable, we can use sets to extract characters from a string in Python.

To extract unique characters from a string in Python, we will first create an empty set. Then, we will iterate through the string characters using a for loop. While iteration, we will add the current character to the set using the add() method. The add() method, when invoked on a set, takes the character as its input argument and adds it to the set if it is not already present there.

After execution of the for loop, we will get all the unique characters of the string in our set. You can observe this in the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=set()
for character in myStr:
    output.add(character)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: {'F', 'e', 'y', 't', 'h', 'P', 'i', 'g', ' ', 'o', 's', 'r', 'n', 'B'}

Instead of using the for loop and add() method, you can use the update() method on a set to extract unique characters from a string in Python. The update() method, when invoked on a set object, takes an iterable object as its input argument. After execution, it adds all the elements of the iterable object to the set. As a string is an iterable object, you can use the update() method to extract unique characters from a string.

To extract unique characters from a string using the update() method, we will first create an empty set using the set() function. Then, we will invoke the update() method on the empty set and pass the string as an input argument to the update() method. After execution, we will get a set containing all the unique characters of the string. You can observe this in the following example.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=set()
output.update(myStr)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: {'F', 'e', 'y', 't', 'h', 'P', 'i', 'g', ' ', 'o', 's', 'r', 'n', 'B'}

You can also directly pass the input string to the set() function to create a set of unique characters of the string as shown below.

myStr="Python For Beginners"
print("The input string is:",myStr)
output=set(myStr)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: {'F', 'e', 'y', 't', 'h', 'P', 'i', 'g', ' ', 'o', 's', 'r', 'n', 'B'}

Suggested Reading: Create Chat Application in Python

Unique Characters From String in Python Using Counter() Method

With all the unique characters, you can also find the frequency of the characters in a string. You can use the Counter function defined in the collections module for this task. 

The Counter() function takes an iterable object as its input argument and returns a Counter object. The Counter object contains all the unique elements of the iterable object and their frequencies.

To extract all the unique characters from a string with their frequency, we will pass the string as the input to the Counter() method. After execution, we will get all the unique characters of the string with their frequency. You can observe this in the following example.

from collections import Counter
myStr="Python For Beginners"
print("The input string is:",myStr)
output=Counter(myStr)
print("The output is:",output)

Output:

The input string is: Python For Beginners
The output is: Counter({'n': 3, 'o': 2, ' ': 2, 'r': 2, 'e': 2, 'P': 1, 'y': 1, 't': 1, 'h': 1, 'F': 1, 'B': 1, 'g': 1, 'i': 1, 's': 1})

Conclusion

In this article, we have discussed different ways to extract unique characters from a string in Python.

To learn more about python programming, you can read this article on string manipulation. You might also like this article on python simplehttpserver.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us