Python string capitalize(), title(), lower(), upper(), swapcase(): Convert lowercase to uppercase and uppercase to lowercase

You can convert a string to titlecase, lowercase, etc in Python.

s = 'i am aN aPPle.'

capitalize = s.capitalize()
title = s.title()
lower = s.lower()
upper = s.upper()
swapcase = s.swapcase()

print(capitalize)  # I am an apple.
print(title)  # I Am An Apple.
print(lower)  # i am an apple.
print(upper)  # I AM AN APPLE.
print(swapcase)  # I AM An AppLE.

The capitalize() literally capitalizes a string. It converts the first letter in a sentence to uppercase and the other letters to lowercase. All words except the first word have lowercases.

On the other hand, the title() capitalizes all the words in a sentence. The first letter in each word is uppercase and the other letters are lowercase.

The lower() makes all the letters in a string lowercase. Similarly, the upper() makes all the lettes uppercase.

The swapcase() is a unique method in Python string. It converts lowercase letters to uppercase and uppercase letters to lowercase. It swaps cases.


Powered by Markdown