You can compare strings directly. x<y means "does x come before y alphabetically?" So you can replace your second block with:

if len(x) == len(y) and x < y:
    return x
Answer from AlwaysBTryin on Stack Overflow
🌐
Quora
quora.com › How-do-you-compare-two-strings-alphabetically-in-Python
How to compare two strings alphabetically in Python - Quora
Use direct comparison operators for simple lexicographic (Unicode) ordering. Use .casefold() for reliable case-insensitive comparisons. Use locale or ICU libraries for proper language-specific alphabetical order.
Discussions

python - Checking if a given string is in alphabetical order - Code Review Stack Exchange
I wrote this function to check if a given string is in alphabetic order. How can I improve? def alphacheck(x): '''This function checks if a given string is in Alphabetical order''' # Crea... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
September 18, 2016
list - Python - arranging words in alphabetical order - Stack Overflow
The program must print the name which is alphabetically the last one out of 8 elements. The names/words can be inputted in any way through code. I think I should be using lists and in range() here.... More on stackoverflow.com
🌐 stackoverflow.com
Why is string "b" bigger than string "azzzzzz" ?
Because “a” comes before “b”. Strings are compared letter by letter. More on reddit.com
🌐 r/learnpython
24
8
November 8, 2021
python - Check if 3 string inputs are in alphabetical order - Stack Overflow
I need to ask for 3 names and compare them after to see if they have been given in alphabetical order or not. I tried things like if (name_1 < name 2) and (name_2 < name_3) but python cannot compare strings like that. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-comparison-in-python
String Comparison in Python - GeeksforGeeks
Explanation: The < and > operators are used to find the order of s1 and s2 lexicographically. This method ideal for sorting and alphabetical comparisons. Strings in Python can be compared case-insensitively by converting both strings to either ...
Published   March 18, 2026
🌐
TutorialsPoint
tutorialspoint.com › article › python-program-to-compare-two-strings-lexicographically
Python Program to Compare two strings lexicographically
July 11, 2023 - We can compare two strings lexicographically in Python using comparison operators like , ==, =. Lexicographic comparison is the process of comparing two strings based on their alphabetical order, similar to how words are arranged in a
🌐
Codingem
codingem.com › home › python string comparison: a step-by-step guide (with examples)
Python String Comparison: A Step-by-Step Guide (with Examples)
July 10, 2025 - ... To compare strings alphabetically, you can use the operators <, >, <=, >=. For instance, let’s compare the names “Alice” and “Bob”. This comparison corresponds to checking if Alice is before Bob in alphabetical order.
🌐
CodeGym
codegym.cc › java blog › learning python › how to compare strings in python
How To Compare Strings in Python
November 11, 2024 - Use <, >, <=, and >= for alphabetical order checks. Use lower() or upper() for case-insensitive comparisons. Use in and not in to check for substring presence. Use locale.strcoll() for locale-sensitive comparisons.
Find elsewhere
🌐
CodeFatherTech
codefather.tech › home › blog › compare strings in python: everything you need to know
Compare Strings in Python: Everything You Need to Know
June 27, 2025 - This second version makes more sense to me logically compared to the first version. ... Try to run the program, its output won’t change. ... Python provides other operators that allow to check which one between two strings comes first alphabetically (< and >).
🌐
Runestone Academy
runestone.academy › ns › books › published › thinkcspy › Strings › StringComparison.html
9.8. String Comparison — How to Think like a Computer Scientist: Interactive Edition
When you compare characters or strings to one another, Python converts the characters into their equivalent ordinal values and compares the integers from left to right.
🌐
IONOS
ionos.com › digital guide › websites › web development › how to compare strings in python
How to compare strings in Python - IONOS
November 21, 2023 - If you don’t want to type any more words, enter \'.\'') if temp == '.': break input_list.append(temp) print('Your entry: ', input_list) i = 0 alph = 1 while(i < len(input_list) - 1): if(input_liste[i] > input_list[i + 1]): print('This list is not sorted alphabetically!') alph = 0 break i = i + 1 if(alph == 1): print('This list is sorted alphabetically.')Python
🌐
Replit
replit.com › home › discover › how to sort a list alphabetically in python
How to sort a list alphabetically in Python | Replit
February 13, 2026 - The sorted() function then calls len() on each item and uses the returned integer—the string's length—for comparison instead of the string's alphabetical value. This arranges the list from the shortest string to the longest.
🌐
Altcademy
altcademy.com › blog › how-to-compare-strings-in-python
How to compare strings in Python - Altcademy.com
June 13, 2023 - In Python, strings can be compared using comparison operators like ==, !=, <, >, <=, and >=. These operators compare the strings based on their lexicographic (dictionary) order, which is the order in which they would appear in a dictionary.
🌐
Miguendes
miguendes.me › python-compare-strings
How to Compare Two Strings in Python (in 8 Easy Ways)
November 28, 2021 - Python treats upper-case and lower-case differently. For example, if we change "maria" to "Maria", then the result is different because M comes before m. >>> name = 'Maria' >>> another_name = 'marcus' >>> name < another_name True >>> ord('M') < ord('m') True >>> ord('M') 77 >>> ord('m') 109 · ⚠️ WARNING ⚠️: Avoid comparing strings that represent numbers using these operators. The comparison is done based on alphabetical ordering, which causes "2" < "10" to evaluated to False.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-check-if-the-characters-of-a-given-string-are-in-alphabetical-order-in-python
5 Best Ways to Check if the Characters of a Given String are in Alphabetical Order in Python – Be on the Right Side of Change
March 8, 2024 - def is_alphabetical(s): for i in range(len(s) - 1): if s[i] > s[i + 1]: return False return True print(is_alphabetical("abcdefg")) print(is_alphabetical("python")) ... This function iterates over the given string, checking if each character comes before the next in the ASCII sequence.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-comparison
Python Compare Strings - Methods & Best Practices | DigitalOcean
April 17, 2025 - This discrepancy occurs because ... Unicode code point value of the characters. In Python, there are three primary methods for comparing strings: ==, is, and cmp()....
🌐
Python Morsels
pythonmorsels.com › lexicographical-ordering
Lexicographical ordering in Python - Python Morsels
February 25, 2026 - When ordering strings lexicographically, we compare the first character in each string, and then the second character, and so on. As long as the characters in the same position are equal, we need to go to the next pair.
🌐
Reddit
reddit.com › r/learnpython › why is string "b" bigger than string "azzzzzz" ?
r/learnpython on Reddit: Why is string "b" bigger than string "azzzzzz" ?
November 8, 2021 -

Hello guys, I got this code:

x = "azzzzzzzzzzzzzz" y = "b"

if x < y: print(y, "is bigger than ",x) else: print(x, "is bigger than ",y)

result is y is bigger than x.

And I don't understand how can x be smaller than y. What is being compared? Does it only compare first letter's ascii table position? Nothing else seems to make sense. Thank you.