Preview
string functions in python 3 with examples
Click here for Quiz on Functions in Python
1. capitalize() method in Python 3
capitalize() function in Python makes first character of string capital.
Example
>>> name='lovejot' >>> name.capitalize() 'Lovejot' #First character becomes capital |
2. find() method in Python 3
find() method in Python 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') 1 #Index of first occurance of ‘o’ is shown i.e. 1. >>> name.find('o',2,7) 5 #Index of ‘o’ in index range 2,7 is shown i.e. 5. |
3. isalnum() method in Python 3
isalnum() method in Python return True if string contains alphabets or digits. There must be at least one character in String.
Example
>>> name='lovejot' >>> name.isalnum() True >>> name1='lovejot2019' >>> name1.isalnum() True |
4. isalpha() method in Python 3
isalpha() method in Python return True if string contains alphabets only. There must be at least one character in String.
Example
>>> name='lovejot' >>> name.isalpha() True >>> name1='lovejot2019' >>> name1.isalpha() False |
5. isdigit() method in Python 3
isdigit() method in Python return True if string contains digits only. There must be at least one character in String.
Example
>>> name='123' >>> name.isdigit() True >>> name1='lovejot2019' >>> name1.isdigit() False |
Click here for Quiz on Functions in Python
6. islower() method in Python 3
islower() method in Python return True if string contains lower case alphabets only. There must be at least one character in String.
Example
>>> name='lovejot' >>> name.islower() True >>> name1='Lovejot' >>> name1.islower() False #First character is capital in ‘Lovejot’. |
7. isupper() method in Python 3
isupper() method in Python return True if string contains upper case alphabets only. There must be at least one character in String.
Example
>>> name='LOVEJOT' >>> name.isupper() True >>> name1='Lovejot' >>> name1.isupper() False #Only first character is Capital |
8. isspace() method in Python 3
isspace() method in Python return True if string contains space only. There must be at least one character in String.
Example
>>> name=' ' >>> name.isspace() True >>> name1=” “ >>> name1.isspace() True |
9. lower() method in Python 3
lower() method in Python converts string value into lowercase. There must be at least one character in String.
Example
>>> name='LOVEJOT ' >>> name.lower() 'lovejot' >>> name1=”LOVEJOT2019“ >>> name1.lower() 'lovejot2019' |
10. upper() method in Python 3
upper() method of Python converts string value into capital letters. There must be at least one character in String.
Example
>>> name='lovejot ' >>> name.upper() 'LOVEJOT' >>> name1=”lovejot2019“ >>> name1.upper() 'LOVEJOT2019' |
11. lstrip() method in Python 3
lstrip() method in Python 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() 'lovejot ' #Spaces removed from the beginning >>> name1='**lovejot**' >>> name1.lstrip('*') 'lovejot**' #*s removed from the beginning. |
12. rstrip() method in Python 3
rstrip() method in Python 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() ' lovejot' #Spaces removed from the end. >>> name1='**lovejot**' >>> name1.rstrip('*') '**lovejot' #*s removed from the end. |
13. strip() method in Python 3
strip() method in Python removes spaces or specified character from both the ends of a string. There must be at least one character in String.
Syntax:
String. strip ([ Char])
Char refers to the character that needs to be removed from the beginning and end of a String. All consecutive occurrences of character will be removed.
Example
>>> name=' lovejot '; >>> name.strip() 'lovejot' #Spaces removed from the beginning as well as end. >>> name1='**lovejot**' >>> name1.strip('*') 'lovejot*' #* removed from the both the ends. |
Click here for Quiz on Functions in Python
Popular Books of Computer Science