string functions in python 3
1. String .capitalize()
This method makes first character of string capital.
Example
>>> name=’lovejot’ >>> name.capitalize() #First character becomes capital |
2. String .find()
This method is used to find first index position of substring in a string value. It returns-1 if substring is not found.
Syntax:
String.find (Substr[,Start[,End]])
Substr refers to substring we want to find in String.
Start refers to starting index.
End refers to ending index of range in which we have to find substring.
Example
>>> name=’lovejot’ >>> name.find(‘o’) #Index of first occurance of ‘o’ is shown i.e. 1. >>> name.find(‘o’,2,7) #Index of ‘o’ in index range 2,7 is shown i.e. 5. |
3. String .isalnum()
This method return True if string contains alphabets or digits. There must be at least one character in String.
Example
>>> name=’lovejot’ >>> name.isalnum() >>> name1=’lovejot2019′ >>> name1.isalnum() |
4. String .isalpha()
This method return True if string contains alphabets only. There must be at least one character in String.
Example
>>> name=’lovejot’ >>> name.isalpha() >>> name1=’lovejot2019′ >>> name1.isalpha() |
5. String .isdigit()
This method return True if string contains digits only. There must be at least one character in String.
Example
>>> name=’123′ >>> name.isdigit() >>> name1=’lovejot2019′ >>> name1.isdigit() |
6. String .islower()
This method return True if string contains lower case alphabets only. There must be at least one character in String.
Example
>>> name=’lovejot’ >>> name.islower() >>> name1=’Lovejot’ >>> name1.islower() #First character is capital in ‘Lovejot’. |
7. String .isupper()
This method return True if string contains upper case alphabets only. There must be at least one character in String.
Example
>>> name=’LOVEJOT’ >>> name.isupper() >>> name1=’Lovejot’ >>> name1.isupper() #Only first character is Capital |
8. String .isspace()
This method return True if string contains space only. There must be at least one character in String.
Example
>>> name=’ ‘ >>> name.isspace() >>> name1=” “ >>> name1.isspace() |
9. String .lower()
This method converts string value into lowercase. There must be at least one character in String.
Example
>>> name=’LOVEJOT ‘ >>> name.lower() >>> name1=”LOVEJOT2019“ >>> name1.lower() |
10. String .upper()
This method converts string value into capital letters. There must be at least one character in String.
Example
>>> name=’lovejot ‘ >>> name.upper() >>> name1=”lovejot2019“ >>> name1.upper() |
11. String .lstrip()
This method removes spaces or specified character from the beginning of a string. There must be at least one character in String.
Syntax:
String. lstrip ([ Char])
Char refers to the character that needs to be removed from the beginning of a String. All consecutive occurrences of character will be removed.
Example
>>> name=’ lovejot ‘; >>> name.lstrip() #Spaces removed from the beginning >>> name1=’**lovejot**’ >>> name1.lstrip(‘*’) #*s removed from the beginning. |
12. String .rstrip()
This method removes spaces or specified character from the end of a string. There must be at least one character in String.
Syntax:
String. rstrip ([ Char])
Char refers to the character that needs to be removed from the end of a String. All consecutive occurrences of character will be removed.
Example
>>> name=’ lovejot ‘; >>> name.rstrip() #Spaces removed from the end. >>> name1=’**lovejot**’ >>> name1.rstrip(‘*’) #*s removed from the end. |