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]

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = True
for 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


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


Output:

Initial Strings list ['aaaaaaaaaaa', 'aaaaaaabaa']
String aaaaaaaaaaa have all characters same
String aaaaaaabaa don't have all characters same


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.