Check whether string contains unique characters in Python

In this tutorial, we will learn to check whether a string contains all the unique characters using a Python program. Many times, we need to take text having unique characters. So, in this case, we need to check the uniqueness of the text entered. Here, we will learn to check the uniqueness of a string and a Python program which will check whether the input string contains all unique characters or not.

Method to check whether a string contains all unique characters

So, we will see a method to check whether a given string contains all unique characters. We have to compare every character with all the characters succeeding it in the string. If any comparison results true, then the string does not contain all unique characters. And if all the comparisons result false, then we can conclude that the string contains all unique characters.

We can check whether a string contains all unique characters by following these steps-

  1. Traverse the string from starting character by character.
  2. Traverse the substring character by character which is at the right of that character.
  3. Compare both the characters.
  4. If the comparison results true, return false to the calling function.
  5. Go to step 2 and repeat until substring is traversed up to the last character.
  6. Go to step 1 and repeat until the string is traversed up to the last character.
  7. Return true to the calling function.
  8. Stop

To understand these steps, let us take an example-

String = "ABCD"

Iteration 1
Character = 'A'
        Character = 'B'
        'A' == 'B' (False)
        Character = 'C'
        'A' == 'C' (False)
        Character = 'D'
        'A' == 'D' (False)
        (Substring traversed till last character)

Iteration 2
Character = 'B'
        Character = 'C'
        'B' == 'C' (False)
        Character = 'D'
        'C' == 'D' (False)
        (Substring traversed till last character)

Iteration 2
Character = 'C'
        Character = 'D'
        'C' == 'D' (False)
        (Substring traversed till last character)

Iteration 3
(String traversed till last character)
Return True

Here, every condition results in false. So, the function returns true to the calling function which means the string contains all the unique characters.

Python program to check whether the string contains all unique characters

Now, we will see a Python program that checks whether the string contains all unique characters. Firstly, we will take the string from the user as an input. Then using the above method, we will check for the uniqueness of the characters in the string. The Python program is-

def check_unique(str):
  for i in range(len(str)):
    for j in range(i + 1,len(str)):
      if(str[i] == str[j]):
        return False
  return True

str = input("ENTER A STRING : ")
if(check_unique(str)):
  print("THE STRING ",str," CONTAINS UNIQUE CHARACTERS")
else:
  print("THE STRING ",str," CONTAINS DUPLICATE CHARACTERS")

So, in the program, the function ‘check_unique’ checks the uniqueness of characters in the string. If the string contains all unique characters, this function returns true. Else, the function returns false which means the string contains at least one duplicate character.

Python program output

The above Python program checks whether a string contains all unique characters or not. The output of the above program after sample execution is given below-

Sample execution with string – “String”

[email protected]:~/python$ python3 unique.py
ENTER A STRING : String
THE STRING  String  CONTAINS UNIQUE CHARACTERS
[email protected]:~/python$

So, the string “String” contains all unique characters.

Sample execution with string – “STRINGS”

[email protected]:~/python$ python3 unique.py
ENTER A STRING : STRINGS
THE STRING  STRINGS  CONTAINS DUPLICATE CHARACTERS
[email protected]:~/python$

So, the string “STRINGS” contains duplicate characters. Because the string contains the character ‘S’ twice.

Thank you for reading this tutorial. I hope it helps you.

Leave a Reply

Your email address will not be published. Required fields are marked *