Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
Can anyone tell me how to compare two strings in Python?

1 Answer

0 votes
by (107k points)

For comparing two strings, you need to first declare your string variables and then use the following operators for comparison:

name1 = 'Henry'

name2 = 'henry'

name3 = 'shawn'

name4 = 'Shawn'

print("Are name1 and name 2 equal?")

print (name1 == name2)

print("Are name and name3 different?")

print (name1 != name3)

print("Is name1 less than or equal to name2?")

print (name1 <= name2)

print("Is name3 greater than or equal to name 2?")

print (name3 >= name2)

print("Is name4 less than name1?")

print (name4 < name1)

The output:

Are name and name 1 equal?

False

Are name and name3 different?

True

Is name less than or equal to name2?

True

Is name3 greater than or equal to name 2?

True

Is name4 less than name?

False

If you are looking for an online course to learn Python, I recommend this Python Certification program by Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked May 6, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...