Skip to Content

Python – Check If All Characters in String are Same

In this tutorial, we will look at how to check if all the characters in a Python string are the same or not with the help of some examples.

How to check if all the characters in a string are the same?

You can use the Python built-in all() function to check if all the characters in a string are equal or not.

check if all characters in a python string are the same

The all() function takes in an iterable as an argument and returns True if all the values in the iterable are truthy (represent True in a boolean context).

So, to check if all the characters in a given string are the same or not, use the all() function to check if each character is equal to the first character in the string. The following is the syntax –

# check if all the characters in a string are same
all(ch == s[0] for ch in s)

It returns True if all the characters in the string are the same. Note that the above comparison is case-sensitive.

There are other methods as well that you can use to accomplish the above task. Some of them are –

  • Iterate through the string and keep track of the distinct characters you encounter. If there’s only one distinct character in the string, you can say that all the characters in the string are the same.
  • Create a set from string characters and check if its size is one or not.

Examples

Let’s now look at some examples of using the above methods. First, we will create a few strings that we’ll use to demonstrate the methods.

# string with all characters same
s1 = "aaaa"
# string with not all same characters
s2 = "aAAa"
# empty string
s3 = ""

# display the strings
print(s1)
print(s2)
print(s3)

Output:

aaaa
aAAa

Here, we created three strings – s1, s2, and s3. The string s1 contains only one distinct character. The string s2 has repeated characters but not all characters are the same and the string s3 is empty.

Example 1 – Using the all() function

The idea here is to use the all() function to check if each character in the string is equal to the first character in the string.

You can use a list comprehension to create a list of boolean values – whether a character is the same as the first character in the string and then pass this resulting list as an argument to the all() function.

Let’s apply this to the strings created above.

# check if all characters in a string are the same
print(all([ch == s1[0] for ch in s1]))
print(all([ch == s2[0] for ch in s2]))
print(all([ch == s3[0] for ch in s3]))

Output:

True
False
True

We get True for s1 and False for s2. Note that we get True for an empty string.

Note that the all() takes an iterable as an argument, you can directly use an iterator (without using a list comprehension).

# check if all characters in a string are the same
print(all(ch == s1[0] for ch in s1))
print(all(ch == s2[0] for ch in s2))
print(all(ch == s3[0] for ch in s3))

Output:

True
False
True

We get the same results as above.

Example 2 – Using a for loop

The idea here is to iterate through the characters in the string and keep a count of the unique characters we encounter. If the resulting count is 1, we can say that all the characters in the string are the same (the string has only one unique character).

def check_if_all_chars_same(s):
    count = 0
    for i in range(len(s)):
        ch = s[i]
        if ch not in s[:i]:
            count += 1
    return count == 1

# check if all characters in a string are the same
print(check_if_all_chars_same(s1))
print(check_if_all_chars_same(s2))
print(check_if_all_chars_same(s3))

Output:

True
False
False

We get True for s1 and False for s2 and s3. Note that here we get False for an empty string.

If you want True as the output for an empty string, modify the condition in the return statement to count <= 1

Example 3 – Using a set

In this method, we create a set from the string characters and check if the size of the set is equal to one. Since a set only holds unique values, if the string has the same characters, the resulting set will only have one value.

# check if all characters in a string are the same
print(len(set(s1)) == 1)
print(len(set(s2)) == 1)
print(len(set(s3)) == 1)

Output:

True
False
False

We get True for s1 and False for s2 and s3. (Same as the above method).

If you want True as the output for an empty string, check if the set length is less than equal to 1.

Summary

In this tutorial, we looked at some different methods to check if all the characters in a string are the same or not. The following are the different methods covered –

  • Use the Python built-in all() function to check if each character in the string is equal to the first character.
  • Iterate through the characters in the string and track the count of unique values encountered.
  • Convert the string to a set and check if its size is equal to one.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.