Count the number of Unique Characters in a String in Python Last Updated : 20 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing duplicates when created from a string. The number of unique characters in string can be determined by finding length of set using len(). Python a = "hello world" # Convert the string to a set u_ch = set(a) # Count the number of unique characters u_c = len(u_ch) print(f"Number of unique characters: {u_c}") OutputNumber of unique characters: 8 Explanation:Code converts the string "hello world" to a set (set(a)) to extract unique characters as sets automatically remove duplicates.It calculates number of unique characters using len(u_ch) and displays count using a formatted string.Using collections.Countercollections.Counter counts character frequencies in a string and stores them as key-value pairs. The number of unique characters is length of Counter object representing distinct keys. Python from collections import Counter a = "hello world" ch = Counter(a) u_c = len(ch) print(f"Number of unique characters: {u_c}") OutputNumber of unique characters: 8 Explanation:Counter(a) counts the frequency of each character in string "hello world" and stores counts in a dictionary-like structure.Number of unique characters is determined using len(ch) which gives total number of distinct keys (characters) in Counter.Using a manual loopA manual loop iterates through each character in string and adds it to a list or set if it is not already present ensuring only unique characters are tracked. The number of unique characters is then determined by counting the length of resulting collection. Python s = "hello world" u_ch = [] for char in s: # If the character is not already in the list, add it if char not in u_ch: u_ch.append(char) # Calculate the number of unique characters u_c = len(u_ch) print(f"Number of unique characters: {u_c}") OutputNumber of unique characters: 8 Explanation:Loop iterates through each character in the string and appends it to the u_ch list only if it hasn't been added before ensuring all characters are unique.Length of u_ch list is calculated using len() representing number of unique characters in string. Comment More infoAdvertise with us Next Article Count occurrences of a character in string in Python T thotasravya28 Follow Improve Article Tags : Python HashSet python-set Practice Tags : pythonpython-set Similar Reads Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Count occurrences of a character in string in Python We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three 2 min read Count the number of times a letter appears in a text file in Python In this article, we will be learning different approaches to count the number of times a letter appears in a text file in Python. Below is the content of the text file gfg.txt that we are going to use in the below programs: Now we will discuss various approaches to get the frequency of a letter in a 3 min read Find all duplicate characters in string in Python In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iteratin 2 min read Calculate the number of characters in each word in a Pandas series To calculate the numbers of characters we use Series.str.len(). This function returns the count of the characters in each word in a series. Syntax: Series.str.len() Return type: Series of integer values. NULL values might be present too depending upon caller series. Another way to find the number of 2 min read Concatenated string with uncommon characters in Python The goal is to combine two strings and identify the characters that appear in one string but not the other. These uncommon characters are then joined together in a specific order. In this article, we'll explore various methods to solve this problem using Python.Using set symmetric difference We can 3 min read Like