DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > String Python Comparisons Along

String Python Comparisons Along

Jake Zimmerman shows us how to string comparison operators in Python, which is a useful feature.

Jake Zimmerman user avatar by
Jake Zimmerman
·
Jul. 28, 16 · Web Dev Zone · Tutorial
Like (4)
Save
Tweet
4.11K Views

Join the DZone community and get the full member experience.

Join For Free

Last time, I talked about Python’s boolean operators and  and or  and what can be confusing about them when “truthy” objects get into the mix. If you haven’t already read it, I would highly recommend it. This article is similar, but looks into something just a little different: the ability to string comparison operators.

Image title

The Confusing Code

Just like last time, we’ll start off looking at some code that confused someone enough to ask the community about it:

'a' in 'abc' # True
True == True # True
'a' in 'abc' == True # False

At a cursory glance, there seems to be something wrong with that it would come up with such a result. But this is caused by a feature of Python that is actually really awesome: the ability to string comparison operators.

Less Confusing Code

This awesome feature of Python that helps us keeps our code more concise allows us to turn this code:

0 < x  and x < 100

Into this much more readable code:

0 < x < 100

Python actually allows you to do this type of thing indefinitely:

0 < x < 100 < y < z

Which gets expanded to:

0 < x and x < 100 and 100 < y and y < z

Except that it’s optimized so that each variable only needs to be looked up once.

The Confusing Part

But it doesn’t just work with the < ,  <= ,  > , and  >=  operators. It also includes  == ,  != ,  is ,  is not , and  in . It’s incredibly useful to include the other operators, but they’re not generally expected to be included (especially not in ).

So, let’s go back to the code example:

'a' in 'abc' == True

Gets effectively expanded to:

'a' in 'abc' and 'abc' == True

To get the effect that we actually were expecting, we need only to wrap the first part in parenthesis:

('a' in 'abc') == True

Outro

Some of you may have learned about this cool feature in Python just now, or you may be like me and forgot about it until this brought it up. You may also have already known about it, but I doubt that you were able to figure out the starting code example right away. The feature is great, and can help us express conditions in a clean way that we learned back in elementary or middle school, but you’ve also seen that it can be confusing.

Just keep this feature in the back of your mind when writing conditionals and make sure you keep it doing what you want it to do. When in doubt, paren’ it out!

Python (language) Comparison (grammar) Strings Data Types

Published at DZone with permission of Jake Zimmerman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Which JVM Version Is the Fastest?
  • DZone's Article Submission Guidelines
  • Checklist for API Verification
  • Java: Why Core-to-Core Latency Matters

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo