Python compare strings (Complete guide)

In this Python tutorial, let us discuss Python compare strings. We will see various examples of python compare two strings. We will see various Python string comparison examples.

  • Python string comparison examples
  • How to compare two strings using == operator in Python
  • How to compare two strings using != operator in Python
  • How to compare two string using > operator in Python
  • How to compare two string using < operator in Python

Python string comparison

  • To compare 2 string, we have python string comparison operators that can be performed using equality (==) and different comparison like (>, <, !=) operators.
  • To compare 2 string, we have to take characters in both strings.
  • The function can take any string values as an input.
  • We need to take to 2 string value. In the below python code, You can see, I have taken the value as “Python is good”.
  • Both the strings are exactly the same, Hence they are equal. So equality operator is returning True as an output.

Python Compare two strings using == operator

Let us see how to compare two string using == operator in Python.

name1 = 'Python is good'
name2 = 'Python is good'
if name1 == name2:
print(name1,'is equal to',name2)
  • After writing the above Python code to check( string equal to ), Ones you will print “name1,’is equal to’,name2” then the output will appear “name1” and “name2” is equal. Also, you can refer to the below screenshot.
Python Compare two strings using == operator
Python Compare two strings using == operator

The above python code we can use to compare two strings using == operator in Python.

Read: Python remove substring from a String

Python Compare two strings using != operator

Let us see how to compare two strings using != operator in Python.

Below represents the python code string not equal to comparison.

name1 = 'Python is good'
name2 = 'Python good'
if name1 != name2:
print(name1,'is NOT equal to',name2)
  • After writing the above Python code to check( string is not equal to ), Ones you will print “name1,’is NOT equal to’,name2” then the output will appear “name1” and “name2” is NOT equal. Also, you can refer to the below screenshot.
Python Compare two strings using != operator
Python Compare two strings using != operator

This is how to compare two strings using != operator in Python.

Read: Python NumPy arange + Examples

Python Compare two string using > operator

Now, we will see how to compare two string functions in Python using greater than comparison operator.

Below represents the python code string is > (greater than) comparison.

name1 = 'Python is good'
name2 = 'Python good'
if name1 > name2:
print(name1,'is greater than',name2)
  • After writing the above Python code to check( string is greater than ), Ones you will print “name1,’is greater than’,name2” then the output will appear. Also, you can refer to the below screenshot.
Python Compare two string using > operator
Python Compare two string using > operator

This is how to compare two string using > operator in Python.

How to compare two string using < operator

Now, we will see how to compare two string functions in Python using less than comparison operator.

Below represents the python code string is < (less than) comparison:

name1 = 'Python good'
name2 = 'Python is good'
if name1 < name2:
print(name1,'is less than',name2)
  • After writing the above Python code to check( string is less than ), Ones you will print “name1,’is less than’,name2” then the output will appear. Also, you can refer to the below screenshot.
less
How to compare two string using < operator

The above python code, we can use to compare two string using < operator in Python.

You may like the following Python tutorials:

In this Python tutorial, we learned about, Python compare strings.

  • Python string comparison
  • Python Compare two strings using == operator
  • Python Compare two strings using != operator
  • Python Compare two string using > operator
  • How to compare two string using < operator