01 September 2016

2 Myths about Python String Comparison

Python String Comparison
Python String Comparison

Comparing Strings 

It’s possible to compare strings just as you would numbers. Keep in mind, however, that Python is picky about strings being equal to each other. If the two strings differ, even slightly, they’re not considered the same.

Consider the following example:

>>> a = "Virginia"
>>> b = "virginia"
>>> a == b False

Although a and b are very similar, one is capitalized and one isn’t. Because they aren’t exactly alike, Python returns False when we ask whether they are alike. Whitespace matters, too.

Consider the following code snippet:

>>> greet1 = "Hello "
>>> greet2 = "Hello"
>>> greet1 == greet2 False greet1 has a space at the end of its string whereas greet2 does not. Python looks at whitespace when comparing strings, so the two aren’t considered equal

1 comment:

Thanks for your message. We will get back you.

Featured post

10 top Blockchain real features useful to financial projects

Blockchain is basically a shared ledger and it has many special features. Why you need it. Business transactions take place every second...

Most Viewed