EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials Python String Tutorial Python Compare Strings

Python Compare Strings

Abhilasha Chougule
Article byAbhilasha Chougule
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated March 31, 2023

Python Compare Strings

Introduction to Python Compare Strings

There are no specific data types for either of the variables in Python if it is numeric or string, or character. A single character is also considered as a string, and it can be declared within double or single quotes. In general, the string is defined as a set of characters or characters within single or double quotes. In Python, the string has any operations and functions on it, such as concatenation, slicing, comparison, len(), max(), min(), etc. In this article, we are discussing the comparison of strings Python.

ADVERTISEMENT
Popular Course in this category
PYTHON Course Bundle - 81 Courses in 1 | 59 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Many operators are used in performing the comparison of strings, such as equality operator (= =), comparison operators like (<, >, <=, >=, !, !=). The simple logic in comparison of strings is that characters are compared to both the strings. It will check the character with a lower Unicode and is considered a smaller value character or larger value character, or equal value character. In this topic, we are going to learn about Python Compare Strings.

How Does String Comparison Work in Python?

In Python, strings use the ASCII value of characters for comparison. Python uses the objects with the same values in memory which makes comparing objects faster. Some basic comparison operator is equal to (= =) and ‘is’ operator. Now let see the example for each of these operators below.

1. The most commonly used comparison operator is equal to (==)

This operator is used when we want to compare two string variables. This can be done using an if statement with equal to (= =) operator.

Syntax:

if string_variable1 = = string_variable2
   true
else
   false
Example #1

Code:

str1 = 'Educba'
str2 = 'Educba'
if str1 == str2:
    print("Strings are same")
else:
    print("Strings are different")

Output:

Python Compare Strings output 1

Example #2

Code:

str1 = 'Educba'
str2 = 'Educba training'
if str1 == str2:
    print("Strings are same")
else:
    print("Strings are different")

Python Compare Strings output 2

The above screenshot is for the above code, which uses equal to (==) operator, which checks the same or different strings.

‘is’ operator is also used for comparison of strings. ‘is’ and equal to (= =) operators are not the same because ‘is’ operator compares two string variables based on the object id it returns true if same string else false if strings are not same.

Syntax:

string_variable1 is string_variable
Example #3

Code:

a = 2
b = 1
c = 2
if a is c:
	print "Same"
else:
	print "Different"

Output:

Python Compare Strings output 3

2. Not equal to (!=) operator

This operator is used to check the not same string variables. This can be done using equal to operator also as shown above.

Example #1

Code:

str1 = "Educba"
str2 = "Educba"
if str1 != str2:
	print"Strings are different"
else:
	print"Strings are same"

Output:

Python Compare Strings output 4

Example #2

Code:

str1 = "Educba"
str2 = "Educba training"
if str1 != str2:
	print"Strings are different"
else:
	print"Strings are same"

Python Compare Strings output 5

In all the above operators, though the strings are the same, then what to do if the order is different to avoid this, we need to first sort the strings and then use them for comparison. This scenario is used when we know that two given string variables are the same, but their order is different, and yet you want the code to print that they are the same strings; to do this, you need to first use the sort function and then compare the given strings.

Syntax:

sorted (string_variable1) = = sorted (string_variable2)
Example #3

Code:

a = "Educab training"
b = "training Educab"
if sorted(a) == sorted(b):
    print"Same"
else:
    print"Not Same"

Output:

Python Compare Strings output 6

3. Greater than (>) and lesser than (<)

These are also used for comparing two strings. These operators use the Unicode values of both the strings to compare each of them accordingly.

Syntax:

string_variable1 > string_variable2
string_variable1 < string_variable2
Example #1

Code:

str1 = "Educba training"
str2 = "Educba"
if str1 > str2:
	print"True"
else:
	print"False"

Output:

Python Compare Strings output 7

If the above code has some strings, it will print as false because either string is smaller or greater.

Example #2

Code:

str1 = "Educba"
str2 = "Educba"
if str1 > str2:
	print"True"
else:
	print"False"

Output:

Python Compare Strings output 8

4. Greater than or equal to ( >=) operator

This operator is used to compare the strings in which the first string is greater or equal to the second string; then, it falsely.

Example #1

Code:

str1 = "Educba training"
str2 = "Educba"
if str1 >= str2:
	print"True"
else:
	print"False"

Output:

output 9

Below is the screenshot for the string 1 is greater than string 2

Example #2

Code:

str1 = "Educba"
str2 = "Educba"
if str1 >= str2:
	print"True"
else:
	print"False"

Output:

output 10

Below is the screenshot for the string 1 is equal to string 2

Example #3

Code:

str1 = "Edu"
str2 = "Educba"
if str1 >= str2:
	print"True"
else:
	print"False"

Output:

output 11

Below is the screenshot for which string1 is smaller than string 2, so it will give false as we are checking for greater than or equal to the string1.

5. less than or equal to (<=)

This also works the same as greater than or equal to, but it will check for the string with lower Unicode among the given strings to compare.

Syntax:

string_variable1 <= string_variable2
Example #1

Code:

str1 = "Edu"
str2 = "Educba"
if str1 <= str2:
	print"True"
else:
	print"False"

Output:

output 12

The below screenshot is for the less than operator to compare whether string1 is less than string 2, then it returns true else false.

Example #2

Code:

str1 = "Edu"
str2 = "Edu"
if str1 <= str2:
	print"True"
else:
	print"False"

Output:

output 13

The below screenshot is for equal to the operator with less than operator; this also yields true if both given strings have the same Unicode values else it will false.

If the string 1 has a higher Unicode value than string 2, it will return false as we are using less than or equal to the operator.

Example #3

Code:

str1 = "Educba"
str2 = "Edu"
if str1 <= str2:
	print"True"
else:
	print"False"

Output:

output 14

Conclusion – Python Compare Strings

In Python, strings are defined as a set of characters where each character has a different Unicode value or ASCII value. There are many different operators which are used to compare the strings, such as equal to (= =) operator, not equal to (!=) operator, greater than (>) operator, less than (<) operator, greater than or equal to operator (>=) and less than or equal to (<=) operator. Another operator is similar to equal to (= =) operator for comparison of strings known as ‘is’ operator, which usually compares the object id of the given strings instead of Unicode values.

Recommended Articles

We hope that this EDUCBA information on “Python Compare Strings” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Python Find String
  2. Python Threadpool
  3. strip Function in Python
  4. Python String Operations
ADVERTISEMENT
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW