Check if All Characters Same

Check if all characters in string are same.

Logic BuildingIntermediate
Logic Building
# Take string input
s = input("Enter a string: ")

# Check if all same
if len(s) == 0:
    print("Empty string")
elif len(set(s)) == 1:
    print("All characters are same")
else:
    print("Characters are different")

Output

Enter a string: aaaa
All characters are same

Enter a string: hello
Characters are different

Use set to check unique characters.

Key Concepts:

  • set(s) gives unique characters
  • If length is 1, all same
  • Otherwise, different