Python | Ways to check string contain all same characters
Given a list of strings, write a Python program to check whether each string has all the characters same or not. Given below are a few methods to check the same.
Method #1: Using Naive Method [Inefficient]
# Python code to demonstrate # to check whether string contains # all characters same or not # Initialising string list ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"] # Printing initial string print ("Initial Strings list", ini_list) # Using Naive Method: flag = Truefor i in ini_list: for j in range(0, len(i)-1): if i[j]!= i[j + 1]: print ("String {} don't have all characters same".format(i)) flag = False break if flag == True: print ("String {} don't have all characters same".format(i)) |
chevron_right
filter_none
Output:
Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa'] String aaaaaaaaaaa don't have all characters same String aaaaaaabaa don't have all characters same
Method #2: Using String Comparisons
# Python code to demonstrate # to check whether string contains # all characters same or not # Initialising string list ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"] # Printing initial string print ("Initial Strings list", ini_list) # Using String comparison for i in ini_list: if i == len(i)*i[0]: print ("String {} have all characters same".format(i)) else: print ("String {} don't have all characters same".format(i)) |
chevron_right
filter_none
Output:
Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa'] String aaaaaaaaaaa have all characters same String aaaaaaabaa don't have all characters same
Method #3: Using count comparison
# Python code to demonstrate # to check whether string contains # all characters same or not # Initialising string list ini_list = ["aaaaaaaaaaa", "aaaaaaabaa"] # Printing initial string print ("Initial Strings list", ini_list) # Using String comparison for i in ini_list: if i.count(i[0]) == len(i): print ("String {} have all characters same".format(i)) else: print ("String {} don't have all characters same".format(i)) |
chevron_right
filter_none
Output:
Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa'] String aaaaaaaaaaa have all characters same String aaaaaaabaa don't have all characters same
Recommended Posts:
- Python | Ways to remove n characters from start of given string
- Python | Ways to check if given string contains only letter
- Check if both halves of the string have same set of characters in Python
- Python | Check if frequencies of all characters of a string are different
- Python program to check if a string contains all unique characters
- Ways to print escape characters in Python
- Python | Ways to split a string in different ways
- Python | Ways to split strings on Uppercase characters
- Reverse string in Python (5 different ways)
- Python | Ways to check if element exists in list
- Find length of a string in python (4 ways)
- Python | Ways to find all permutation of a string
- Python | Ways to concatenate boolean to string
- Ways to remove i'th character from string in Python
- Python | Extract only characters from given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

