Count the number of Unique Characters in a String in Python Last Updated : 20 Feb, 2025 Comments Improve Suggest changes 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 the number of Unique Characters in a String in Python T thotasravya28 Follow Improve Article Tags : Strings Hash Technical Scripter Python Python-Quizzes DSA HashSet python-set +4 More Practice Tags : Hashpythonpython-setStrings Similar Reads Count the number of unique characters in a given String Given a string, str consisting of lowercase English alphabets, the task is to find the number of unique characters present in the string. Examples: Input: str = âgeeksforgeeksâOutput: 7Explanation: The given string âgeeksforgeeksâ contains 7 unique characters {âgâ, âeâ, âkâ, âsâ, âfâ, âoâ, ârâ}. Inp 14 min read 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 Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre 3 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 Count of all unique substrings with non-repeating characters Given a string str consisting of lowercase characters, the task is to find the total number of unique substrings with non-repeating characters. Examples: Input: str = "abba" Output: 4 Explanation: There are 4 unique substrings. They are: "a", "ab", "b", "ba". Input: str = "acbacbacaa" Output: 10 App 6 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 Count substrings made up of a single distinct character Given a string S of length N, the task is to count the number of substrings made up of a single distinct character.Note: For the repetitive occurrences of the same substring, count all repetitions. Examples: Input: str = "geeksforgeeks"Output: 15Explanation: All substrings made up of a single distin 5 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 Count of substrings containing only the given character Given a string S and a character C, the task is to count the number of substrings of S that contains only the character C.Examples: Input: S = "0110111", C = '1' Output: 9 Explanation: The substrings containing only '1' are: "1" â 5 times "11" â 3 times "111" â 1 time Hence, the count is 9. Input: S 6 min read Like