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.
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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 –
- Python – Check If String Is Empty – With Examples
- Python – Check If String Contains Only Letters and Numbers
- Python – Check If All Elements in List are Strings
- Python – Check If All Elements in a List are Equal
- Python – Check If All Elements in a List are Unique
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.