Chapter 15: Some useful function and methods

In this chapter, we will explore some commonly used functions and methods in Python for string manipulation.

 

len() Function: Returns the length of a string, including spaces.

name   = "Free Learning"

length = len(name) # Total Length

print(length)

Ouptut: 13
 

lower() Method: Converts all characters in a string to lowercase.

name      = "FreE Learning"
namelower = name.lower() # Lower Case
print(namelower)
Ouput: free learning
 

upper() Method: Converts all characters in a string to uppercase.

name      = "Free Learning"
nameupper = name.upper() # Upper Case
print(nameupper)
Ouput: FREE LEARNING
 
title() Method: Capitalizes the first letter of each word in a string.
name      = "FreE Learning"
nametitle = name.title() # Sentence Case
print(nametitle)

Output: Free Learning

 
count() Method: Counts the occurrences of a specified character in a string.
name    = "FreE Learning"
count_e = name.count('e') # case-sensitive
print(count_e)
Ouput: 2
 
Note: Methods like lower(), upper(), title(), and count() are used with the dot operator (.).