Previous Index Next

The string methods

Python has a set of built-in methods that you can use on strings.

isdigit()

The method checks whether the strings consist only of digits and returns true or false otherwise.

>>> str ='077'
>>> str.isdigit()
True
>>> str ='0A7'
>>> str.isdigit()
False

isalnum()

Return true if all characters in the string are alphanumeric, false otherwise.

>>> str ='B001'
>>> str.isalnum()
True
>>> str ='A$12'
>>> str.isalnum()
False

isalpha()

Return true if all characters in the string are alphabetic, false otherwise.

>>> str ='banana'
>>> str.isalpha()
True
>>> str ='B001'
>>> str.isalpha()
False

islower()

Return true if all characters in the string are in lowercase, false otherwise.

>>> str ='banana'
>>> str.islower()
True
>>> str ='Banana'
>>> str.islower()
False

isupper()

Return true if all characters in the string are in uppercase, false otherwise.

>>> str ='BANANA'
>>> str.isupper()
True
>>> str ='Banana'
>>> str.isupper()
False

lower()

Return string after converting every character of the string into lower case.

>>> str ='HELLO'
>>>str.lower()
'hello'

upper()

Return string after converting every character of string into uppercase.

>>> str ='hello'
>>> str.upper()
'HELLO'

capitalize()

Return string where the first character is upper case.

>>> str = 'python string methods'
>>> str.capitalize()
'Python string methods'

title()

Return string where the first character in every word is upper case.

>>> str = 'python string methods'
>>> str.title()
'Python String Methods'

lstrip()

Return string after removing all the characters from the beginning of the string. (space is the default character to remove)

>>> str ='   Hello'
>>> str.lstrip()
'Hello'
>>> str ='aaaaaaaaaHello'
>>> str.lstrip('a')
'Hello'

rstrip()

Returns string after removing all the characters from the end of the string. (space is the default character to remove)

>>> str ='Hello      '
>>> str.rstrip()
'Hello'
>>> str ='Hellozzzzzz'
>>> str.rstrip('z')
'Hello'

strip()

Return string with the leading and trailing characters removed.

>>> str ='    Hello   '
>>> str.strip()
'Hello'

find(substring, start, end)

Return the index of first occurrence in the string where substring is found. Returns -1 if the value is not found. If we do not give start index and end index then searching starts from index 0 and ends at length of the string

>>> str ='banana'
>>> str.find('an')
1
>>> str.find('an',2,6)
3
>>> str.find('ox')
-1

index(substring, start, end)

Return the index of first occurrence in the string where substring is found. Raises an exception if the substring is not present in the given string. If we do not give start index and end index then searching starts from index 0 and ends at length of the string

>>> str ='banana'
>>> str.index('an')
1
>>> str.index('an',2,6)
3

count(substring, start, end)

Return number of times substring occurs in the given string. If we do not give start index and end index then searching starts from index 0 and ends at length of the string

>>> str = 'banana'
>>> str.count('an')
2
>>> str.count('an',2,6)
1

replace(old, new)

This method returns the copy of the string in which a certain word is replaced by the given word.

>>> str ='A2009'
>>> str.replace('0','*')
'A2**9'

startswith(substring)

Return True if the given string starts with the supplied substring otherwise returns False

>>> str = 'Hello World!'
>>> str.startswith('He')
True
>>> str.startswith('Hi')
False

endswith(substring)

Return True if the given string ends with the supplied substring otherwise returns False

>>> str = 'Hello World!'
>>> str.endswith('!')
True
>>> str.endswith('?')
False

split()

Return a list of words delimited by the specified substring. If no delimiter is given then words are separated by space.

>>> s = 'Just drink more coffee.'
>>> s.split()
['Just', 'drink', 'more', 'coffee.']
>>> str = 'A@B23@Z'
>>> str.split('@')
['A', 'B23', 'Z']

join()

Join all items of tuple/list into one string. String must be specified as the separator.

>>> '#'.join(['A', 'B23', 'Z'])
'A#B23#Z'

partition(substring)

The partition() method searches for a specified string, and splits the string into a tuple containing the part the before separator, argument string and the part after the separator.

>>> str = 'I love to code!'
>>> str.partition('to')
('I love ', 'to', ' code!')

Previous Index Next