upper(), lower(), capitalize(), title(), and swapcase()

String methods such as upper(), lower(), capitalize(), title(), and swapcase() help when we wish to convert all the string elements into an entire series. The upper and lower methods convert the entire string into uppercase or lowercase. The following command shows converting a series into uppercase:

sample_df["Movie"].str.upper()

The following is the output:

Converting a series into uppercase

The following command shows converting a series into lowercase:

sample_df["Movie"].str.lower()

The following is the output:

Converting a series into lowercase

The capitalize() method converts the first letter into uppercase and the rest into lowercase:

pd.Series(["elon musk", "tim cook", "larry page", "jeff bezos"]).str.capitalize()

The following is the output:

Capitalize function for a series

The title() method ensures that the first letter of each word of a string is capitalized while the rest are in lowercase:

pd.Series(["elon musk", "tim cook", "larry page", "jeff bezos"]).str.title()

Title case conversion for a string

The swapcase() method switches uppercase to lowercase and vice versa:

sample_df["Movie"].str.swapcase()

The following is the output:

The swapcase() function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset