Skip to content
Related Articles

Related Articles

Python | Ways to check string contain all same characters

Improve Article
Save Article
  • Last Updated : 25 Nov, 2022
Improve Article
Save Article

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] 

Python3




# 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 {} have all characters same".format(i))

Output

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

  Method #2: Using String Comparisons 

Python3




# 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))

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 

Python3




# 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))

Output

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

Method #4 : Using len() and set() methods

Python3




# 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:
    a = len(set(i))
    if(a == 1):
        print("String {} have all characters same".format(i))
    else:
        print("String {} don't have all characters same".format(i))

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
Related Articles

Start Your Coding Journey Now!