Python String Comparison

python-strings-comparison-ipcisco

We can compare strings in python. So, how can we do python string comparison? We can compare python strings with different signs like equality (==) and comparison (<, >, <=, >= , !=) operators.

 

Here, the comparison of the strings is done through unicode values. The lower unicode value is considered to be smaller.

 

Let’s do an example for string comparison and see the return of the python code for differetn situations.

 

msg = 'Abc'
 print(msg == 'Abc')
print(msg != 'Abc')
print(msg < 'Abc')
print(msg > 'Abc')
print(msg <= 'Abc')
print(msg >= 'Abc')

 

The return of this python code will be like below:

 

True
False
False
False
True
True

 

 


You can also learn How to Split Python Strings.


 

python-strings-comparison-ipcisco

 

The first code returns true and the second code returns false, because the strings are same and equal. The third and fourt codes will return false and the last two codes will return true. Because the last two one has also equal sign.

 

For different purposes, you can use python string comparison in your python codes. With this functions, you can compare the strings and get returns about these strings. You can increase these examples by yourself to understant this comparison lesson better.

 

Back to: Python Programming Course > Python Strings

Leave a Reply

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

IPCisco is the Winner of 2019 & Finalist of 2020 in “Best Certification Study Journey ” Category!

Cisco-ITBlogAwards-2019-Winner-IPCisco-k
ITBlogAwards_2020_Badge-Finalist-BestCertStudy-ipcisco

Python Programming Course

Collapse
Expand
IPCisco on Social Media!

Latest Blog Posts

metaverse-jobs-ipcisco.com
Wifi-6-vs-wifi-5-ipcisco.com
16-Reasons-To-Learn-Linux
gns3-vm-ware-installation-guide
reasons-to-have-cisco-ccna-ipcisco-1