Check if all characters in a String are the same in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if all characters in a String are the same in Python #

To check if all characters in a string are the same, compare the string to its first character multiplied by the string's length. If multiplying the first character by the string's length returns True, all characters in the string are the same.

main.py
my_str = 'bbb' all_same = my_str == my_str[0] * len(my_str) print(all_same) # 👉️ True if all_same: # 👇️ this runs print('All characters in the string are the same') else: print('NOT all characters in the string are the same')
We used the equality (==) operator to compare the string to its first character multiplied by the string's length.

The multiplication operator can be used to repeat a string a specified number of times.

main.py
my_str = 'bbb' print(my_str[0] * len(my_str)) # 👉️ 'bbb' print('b' * 3) # 👉️ 'bbb'

We simply create a string of the same length by repeating the first character of the original string.

If the original string is equal to the new string, then all characters in the string are the same.

If you need to handle a scenario where the string might be empty, use the and boolean operator.

main.py
my_str = '' all_same = my_str and my_str == my_str[0] * len(my_str) print(all_same) # 👉️ '' if all_same: print('All characters in the string are the same') else: # 👇️ this runs print('NOT all characters in the string are the same')

We used the boolean and operator, so for the if block to run, both conditions have to be met.

This is the most performant approach for checking if a string consists of one character.

Alternatively, you can use the all() function.

Check if all characters in a String are the same using all() #

To check if all characters in a string are the same:

  1. Use a generator expression to iterate over the string.
  2. Compare each character to the first character in the string.
  3. If the condition is met for all characters, all characters in the string are the same.
main.py
my_str = 'bbb' all_same = all(char == my_str[0] for char in my_str) print(all_same) # 👉️ True

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we compare the current character to the first character in the string and return the result.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

If the condition returns False for any of the characters in the string, the all() function short-circuits returning False.

Note that the all() function returns True if the iterable is empty.

main.py
my_str = '' all_same = all(char == my_str[0] for char in my_str) print(all_same) # 👉️ True

You can check for the string's length if you need to handle this scenario.

main.py
my_str = '' all_same = my_str and all(char == my_str[0] for char in my_str) print(all_same) # 👉️ '' if all_same: print('All characters in the string are the same') else: # 👇️ this runs print('NOT all characters in the string are the same')

The if block runs only if the string is not empty and all characters in the string are the same.

Alternatively, you can use the str.count() method.

Check if all characters in a String are the same using str.count() #

To check if all characters in a string are the same:

  1. Use the str.count() method to count the occurrences of the first character in the string.
  2. If the number of occurrences of the first character is equal to the string's length, then all characters in the string are the same.
main.py
my_str = 'bbb' all_same = my_str.count(my_str[0]) == len(my_str) print(all_same) # 👉️ True

The str.count method returns the number of occurrences of a substring in a string.

If the number of occurrences of the first character in the string is equal to the string's length, then the string consists of one character.

You can also use the set() class to achieve the same result.

Check if all characters in a String are the same using set() #

To check if all characters in a string are the same:

  1. Use the set() class to convert the string to a set.
  2. Use the len() function to get the length of the set.
  3. If the set has a length of 1, then all characters in the string are the same.
main.py
my_str = 'bbb' all_same = len(set(my_str)) == 1 print(all_same) # 👉️ True

We used the set() class to convert the string to a set object.

main.py
my_str = 'bbb' print(set(my_str)) # 👉️ {'b'}

Set objects are an unordered collection of unique elements, so any duplicate characters get removed when converting to a set.

If the set has a length of 1, then all characters in the string are the same.

Which approach you pick is a matter of personal preference. I'd use the first approach if prioritizing performance.

On the other hand, I find converting the string to a set to be the most readable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.