Comparing Strings In Python

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/comparing-strings-python/
PINTEREST
PINTEREST
INSTAGRAM

String is very useful and popular variable type used in python programming language. Sometimes we need to operate two or more strings and compare them. Comparing numbers is easy but string is a bit different and harder. In this tutorial we will look different comparison operations on Python strings.

Find Bigger String

Two numbers can be compared in order to find the bigger one but how can we find bigger string. Strings are consist of multiple characters and evaluated according to these multiple characters. Comparing two string means comparing ASCII values of each characters one by one. For example:

Find Bigger String

Find Bigger String

This two strings are very similar but third character is different for both. If we compare these two strings b will be bigger than a variable because of d ASCII value is bigger than c ASCII value. And as a result boolean value True is returned

Check If Same

The most popular comparison operation is checking the two strings if they are same. We will use == operator in order check equality.

Check If Same

Check If Same

Check If Different

The reverse of checking equality is checking if two strings are different. If the strings are different from each other this will return boolean True . We will use != operator for this operations.

Check If Different

Check If Different

Check If String Empty

Strings can provide different characters but also can contain no value or just spaces. We can check if a string is empty or not by using strip function. We will strip spaces from the string and check equality with empty string literal like below.

LEARN MORE  How To Check If String Contains A Specific Word in Php?

Check If String Empty

Check If String Empty

We can also use string with the if keyword. If string have a value different than space or empty the if will get a True boolean value.

Check If String Empty

Check If String Empty

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/comparing-strings-python/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)