Social Media

1/02/2019

Functions Related to string

len() method: This function is similar to the len() function we have used in case of list data structure.
This will give us the length of the string.
s="mayank"
print(len(s)) #6

upper() method: This method is used to convert all characters of string into uppercase. The original string is not affected by this method.
s1="mayank"
s2=s1.upper()
print(s1) #mayank
print(s2) # MAYANK

lower() method: It converts all the characters of string into lower case. It is similar to upper() method and does not affect the original string.
s1="Mayank"
s2=s1.lower()
print(s1) #Mayank
print(s2) # mayank

startswith() and endswith() method: This method returns True or False. If the string starts with the supplied sequence it will return True otherwise return False.
s="Mayank"
s.startswith("Ma") #True
Similarly ends with check the ending of the string with given sequence.
s.endswith("an") #False

split() function: The return type of this function is a list. String is separated on the basis of given character. This character will not be included in the final list.
s="mayank kumar prajpati"
l=s.split(" ")
print(l) #["mayank","kumar","prajapati"]



Be First to Post Comment !
Post a Comment