In this Python tutorial, you will learn how to sort a list of strings in ascending or descending order using the list.sort() method and the sorted() built-in function. You will also learn how to sort strings without changing the original list, ignore letter case, and sort by string length.

Sort a List of Strings in Python

Python compares strings lexicographically, one character at a time. For a basic list of lowercase words, this produces alphabetical order. The two main approaches are:

  • list.sort() sorts a list in place and changes the original list.
  • sorted() returns a new sorted list and leaves the original iterable unchanged.
RequirementRecommended approach
Change the existing listwords.sort()
Keep the original list unchangedsorted(words)
Sort from Z to AUse reverse=True
Ignore uppercase and lowercase differencesUse key=str.casefold
Sort by number of charactersUse key=len

Sort Strings In Place with list.sort()

sort() is a list method. It rearranges the items in the same list and returns None. Use it when you no longer need the list in its original order.

The syntax of list.sort() is:

</>
Copy
 sort(*, key=None, reverse=False)
  • key specifies a function whose result is used for each comparison.
  • reverse=True sorts the result in descending order.

Sort Lowercase Strings in Ascending Order

In the following example, sort() arranges the strings from smallest to largest according to Python’s string comparison rules.

Python Program

</>
Copy
words = ["car", "apple", "banana", "fan", "dog"]

#sort list in ascending order
words.sort()

print(words)

Output

['apple', 'banana', 'car', 'dog', 'fan']

The original words list now contains the sorted items.

Reference tutorial

Understand How Uppercase and Lowercase Strings Are Ordered

Python string sorting is case-sensitive by default. Uppercase and lowercase letters have different Unicode code points, so words beginning with uppercase letters can appear before lowercase words.

Python Program

</>
Copy
words = ["car", "apple", "banana", "Car"]

#sort list in ascending order
words.sort()

print(words)

Output

['Car', 'apple', 'banana', 'car']

This result is not a case-insensitive dictionary order. To ignore case during comparison, supply a suitable key function.

Sort Strings Without Considering Letter Case

The key argument transforms each string only for comparison. It does not replace or permanently change the strings stored in the list.

The existing example uses str.upper as the comparison key:

Python Program

</>
Copy
words = ["car", "apple", "banana", "Car"]

#sort list in ascending order
words.sort(key=str.upper)

print(words)

In key=str.upper, Python calls str.upper() for each list item and compares the returned uppercase values. The original strings remain unchanged.

Output

['apple', 'banana', 'car', 'Car']

For general case-insensitive sorting, str.casefold is usually more suitable because it is designed for caseless text comparison.

</>
Copy
words = ["Banana", "apple", "Car", "car"]

words.sort(key=str.casefold)

print(words)

Output

['apple', 'Banana', 'Car', 'car']

Sort Strings in Descending Alphabetical Order

By default, list.sort() sorts in ascending order. Pass reverse=True to reverse the final ordering.

Python Program

</>
Copy
words = ["car", "apple", "banana"]

#sort list in descending order
words.sort(reverse=True)

print(words)

Output

['car', 'banana', 'apple']

Return a New Sorted String List with sorted()

sorted() accepts any iterable, including lists, tuples, sets, and generators. It always returns a new list. This makes it useful when the original order must be preserved.

</>
Copy
 sorted(iterable, *, key=None, reverse=False)

Sort Strings While Keeping the Original List

In this example, sorted(words) creates a new list named sortedList. The original words list keeps its existing order.

Python Program

</>
Copy
words = ["car", "apple", "banana"]

#sort list in ascending order
sortedList = sorted(words)

print(words)
print(sortedList)

Output

['car', 'apple', 'banana']
['apple', 'banana', 'car']

The first output line is the unchanged input list. The second line is the newly created sorted list.

Reference tutorial

Use sorted() for Descending or Case-Insensitive Order

The sorted() function supports the same key and reverse arguments as list.sort().

</>
Copy
words = ["Banana", "apple", "Car"]

result = sorted(words, key=str.casefold, reverse=True)

print(result)

Output

['Car', 'Banana', 'apple']

Sort a Python List of Strings by Length

To order strings by their number of characters, pass len as the key function. Python compares the integer length returned for each string.

</>
Copy
words = ["watermelon", "fig", "apple", "kiwi"]

words.sort(key=len)

print(words)

Output

['fig', 'kiwi', 'apple', 'watermelon']

Use reverse=True with key=len to place the longest strings first.

Sort Strings by Length and Then Alphabetically

When several strings have the same length, a tuple key can add a second sorting rule. In the following example, Python first compares string length and then compares lowercase text.

</>
Copy
words = ["pear", "fig", "Apple", "kiwi", "plum"]

result = sorted(words, key=lambda word: (len(word), word.casefold()))

print(result)

Output

['fig', 'kiwi', 'pear', 'plum', 'Apple']

Difference Between list.sort() and sorted() for Strings

Both approaches use the same sorting rules and accept key and reverse. The main difference is whether the original list is changed.

Featurelist.sort()sorted()
Accepted inputLists onlyAny iterable
Changes the original listYesNo
Return valueNoneA new list
Extra list allocationNo new result listCreates a new result list
Supports key and reverseYesYes

Common Mistakes When Sorting String Lists

  • Assigning the result of sort(): words = words.sort() makes words equal to None. Call words.sort() without assignment.
  • Expecting case-insensitive order: use key=str.casefold when letter case should not affect comparisons.
  • Changing the original list unintentionally: use sorted(words) when the initial order must remain available.
  • Calling str.upper() inside the key argument: pass the function as key=str.upper, without parentheses.

Frequently Asked Questions About Sorting Python Strings

Does sort() return the sorted list?

No. list.sort() changes the list in place and returns None. Use the list after calling sort(), or use sorted() when you need a returned list.

How do I sort strings from Z to A in Python?

Pass reverse=True to either words.sort() or sorted(words).

How do I sort strings without considering uppercase and lowercase?

Use key=str.casefold, for example sorted(words, key=str.casefold).

How do I sort strings by length in Python?

Use key=len, such as words.sort(key=len) or sorted(words, key=len).

Summary of Python String List Sorting

Use list.sort() to rearrange an existing list and sorted() to create a new sorted list. Add reverse=True for descending order, key=str.casefold for case-insensitive comparisons, or key=len to sort by string length. In this Python Tutorial, you learned how each option affects the resulting list.