Tutorials  /  Python

How To Compare Strings in Python

Ccentron Redaktion · December 2024 ·3 min read ·Python, Tutorial

Introduction

You can compare strings in Python using the equality (==) and comparison (<, >, !=, <=, >=) operators. There are no special methods to compare two strings. In this article, you’ll learn how each of the operators work when comparing strings.

Python string comparison compares the characters in both strings one by one. When different characters are found, then their Unicode code point values are compared. The character with the lower Unicode value is considered to be smaller.

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Strings in Python Equality and Comparison Operators

Declare the string variable:

Code
fruit1 = 'Apple'

The following table shows the results of comparing identical strings (Apple to Apple) using different operators.

Operator Code Output
Equality print(fruit1 == 'Apple') True
Not equal to print(fruit1 != 'Apple') False
Less than print(fruit1 < 'Apple') False
Greater than print(fruit1 > 'Apple') False
Less than or equal to print(fruit1 <= 'Apple') True
Greater than or equal to print(fruit1 >= 'Apple') True

Both the strings are exactly the same. In other words, they’re equal. The equality operator and the other equal to operators return True.

If you compare strings of different values, then you get the exact opposite output.

If you compare strings that contain the same substring, such as Apple and ApplePie, then the longer string is considered larger.

Comparing User Input to Evaluate Equality Using Operators

This example code takes and compares input from the user. Then the program uses the results of the comparison to print additional information about the alphabetical order of the input strings in Python. In this case, the program assumes that the smaller string comes before the larger string.

Python

fruit1 = input('Enter the name of the first fruit:\n')
fruit2 = input('Enter the name of the second fruit:\n')

if fruit1 < fruit2: print(fruit1 + " comes before " + fruit2 + " in the dictionary.") elif fruit1 > fruit2:
    print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
    print(fruit1 + " and " + fruit2 + " are the same.")
  

Here’s an example of the potential output when you enter different values:

Code

Output
Enter the name of first fruit:
Apple
Enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.
  

Here’s an example of the potential output when you enter identical strings:

Code

Output
Enter the name of first fruit:
Orange
Enter the name of second fruit:
Orange
Orange and Orange are the same.
  

Note: For this example to work, the user needs to enter either only upper case or only lower case for the first letter of both input strings. For example, if the user enters the strings apple and Banana, then the output will be apple comes after Banana in the dictionary, which is incorrect.

This discrepancy occurs because the Unicode code point values of uppercase letters are always smaller than the Unicode code point values of lowercase letters: the value of a is 97 and the value of B is 66. You can test this yourself by using the ord() function to print the Unicode code point value of the characters.

Conclusion for Strings in Python

In this article, you learned how to compare strings in Python using the equality (==) and comparison (<, >, !=, <=, >=) operators. Continue your learning about Strings in Python.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren