Thursday, 19 September 2019

Python - String's Case Handling



In this article, we will learn the various ways to handle the case of the alphabets

Series.str can be used to access various functions for String


#Import necessary libraries
import pandas as pd
import numpy as np

#Create a Series 
s_case = pd.Series(['apple','Pomogranate',np.nan,'strawberry','the charlie and the chocolate factory'])
print(s_case)

Output:

#Capitalize the first character of the string
print("Use Captalize")
print(s_case.str.capitalize())

Output:












#Converts the character in lowercase
print("Use lowercase")
print(s_case.str.lower())

Output:











#Converts the charcter in uppercase
print("Use uppercase")
print(s_case.str.upper())

Output:











#Swaps the case from lower to upper and vice versa
print("Use swapcase")
print(s_case.str.swapcase())

Output:











#Converts the string into title case
print("Use title")
s_case.str.title()


Output:












#Check if the string is lower case
print("Use islower()")
print(s_case.str.islower())

Output:











Like wise there are many other functions like isupper(), isnumeric(), isalpha()

No comments:

Post a Comment