String Functions Class11
String Functions Class11
String Functions Class11
1. len() function:
It returns the length of a string.
Syntax: len(<string_name>)
Eg: name = “Hello”
print(len(name)) returns 5
3. count() method:
It returns the number of occurrence of a substring in a string or string slice.
Syntax: <string>.count(sub,start,end)
Eg: ‘abcdabacab’.count(‘ab’)
‘abcdabacab’.count(‘ab’,4,8)
‘abcdabacab’.count(‘ab’,4)
4. find() method:
Returns the lowest index in the string where a substring is found.
Returns -1 if not found.
Syntax: <string>.find(sub, start,end)
Eg: ‘abcdaba’.find(‘ab’) returns 0
5. index() method:
Returns the lowest index in the string where a substring is found.
Results in ValueError if not found.
‘abcdaba’.index(‘ab’) returns 0
‘abcdaba’.index(‘ab’,2,5) ValueError
6. isalnum()method:
- Returns True if characters in a string are alphanumeric (alphabet or
number)
- Space character is not an alphanumeric.
Eg: ch = ‘abcde’
ch.isalnum() returns True.
7. isalpha() method:
Returns True if characters in a string are alphabets only.
Returns False otherwise.
‘abc123’.isalpha() returns False
‘abcd’.isalpha() returns True
8. isdigit() method:
Returns True if all characters in a string are digits only, otherwise
returns False.
Eg: ‘abcd123’.isdigit() returns False
‘1234’.isdigit() returns True
9. islower() method:
Returns True if all the characters in a string are of lowercase, other
wise returns False.
Eg: ‘abcd’.islower() returns True
‘Abcd’.islower() returns False
10. isupper() method:
Returns True if all the characters in the string are of uppercase.
Eg: ‘abcd’.isupper() returns False
‘ABCD’.isupper() returns True.
11.isspace() method:
Returns True if the string contain only space “ ”.
“ ”.isspace() returns True
“”.isspace() returns False since no space.
• 12. upper()
Converts all the characters in a string to uppercase.
Eg: ‘hello’.upper() returns ‘HELLO’
ch = “world”
ch.upper() returns ‘WORLD’
13. lower()
Converts all the characters in a string to lower case.
Eg: ‘HELLO’.lower() returns ‘hello’
‘World’.lower() returns ‘world’
14. lstrip(),rstrip(),strip()
lstrip() - Whitespaces from leftmost end are removed.
Eg: “ abcd ”.lstrip() returnd “abcd ”