• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Compare Strings Alphabetically

Using Python to Compare Strings Alphabetically

June 2, 2022 Leave a Comment

To compare strings alphabetically in Python, you can use the < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) operators.

a = "this is a string"
b = "another string"

if a < b:
   print("a is less than b")
else:
   print("a is greater than or equal to b")

#Output:
a is greater than or equal to b

Note that uppercase letters come before lowercase letters.

a = "this"
b = "This"

if a < b:
   print("a is less than b")
else:
   print("a is greater than or equal to b")

#Output:
a is less than b

When working with strings, sometimes it can be useful to compare strings alphabetically. You can easily compare strings in Python.

The < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) operators work just like they work with numbers. String comparison, using these operators, compares the unicode representation of the characters.

Below are some examples of comparing strings alphabetically in Python.

print("this" < "word")
print("word" < "this")
print("another" <= "word")
print("another" <= "another")

#Output:
True
False
True
True

Comparing Strings Alphabetically in Python

As mentioned above, when comparing strings, Python is comparing the unicode representation of the characters from left to right.

When working with strings which have uppercase and lowercase characters, you have to be careful since uppercase characters come before lowercase characters in unicode.

You can see the unicode value of a character with the Python ord() function. Below shows you the difference between an uppercase and lowercase “a”.

print(ord("a"))
print(ord("A"))

#Output:
97
65

If you are comparing strings with a mix of uppercase and lowercase letters, then it might make sense to use either the lower() or upper() functions to convert your string to have all uppercase or all lowercase characters.

uppercase = "HELLO"
lowercase = "hello"

print(uppercase < lowercase)
print(uppercase.lower() < lowercase.lower())
print(uppercase.lower() == lowercase.lower())

#Output:
True
False
True

Hopefully this article has been helpful for you to learn how to compare strings alphabetically using Python.

Other Articles You'll Also Like:

  • 1.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 2.  Print Multiple Variables in Python
  • 3.  Using Python to Count Items in List Matching Criteria
  • 4.  Read Pickle Files with pandas read_pickle Function
  • 5.  Python Round to Nearest 10 With round() Function
  • 6.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 7.  Fibonacci Sequence in Python with for Loop
  • 8.  Multiple Condition While Loops in Python
  • 9.  Python Check if Attribute Exists in Object with hasattr() Function
  • 10.  Print Time Elapsed in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy