Table of Contents
String methods in python
Python supports various compound datatypes like String, tuple,dictionary etc.
In previous post we studied about how strings are immutable and what are string operations?
In this post we will be concentrating on various string methods.
String consist of various methods for easy functioning and utilization of python script.
Many methods like find(), capitalize(), upper() lower() len() etc are used to provide various functionalities and can be used again and again as per need without need of coding again this helps in reducing redundancy.
List of string methods
- capitalize()
- It returns a capitalized version of string, i.e. make the first character have upper case and the rest lower case.
S.capitalize()
- count()
- It returns the number of occurrences of sub string in string S[start:end]. Optional arguments start and end are interpreted as in slice notation
S.count(sub[, start[, end]])
- endswith()
- Return True if String ends with the specified suffix, False otherwise.
S.endswith(suffix,start=0,end=len(string))
- find()
- It returns the lowest index in String where sub string is found,such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.
- It returns -1 if no match found.
S.find(str,start=0,end=len(string))
- index()
- It is like S.find() but raises ValueError when the sub string is not found.
S.index(str,start=0,end=len(string))
- isalnum()
- It returns True if all characters in String are alphanumeric and there is at least one character in String, False otherwise.
S.isalnum()
- isalpha()
- It returns True if all characters in String are alphabetic and there is at least one character in String, False otherwise.
S.isalpha()
- isdigit()
- It returns True if all characters in String are digits and there is at least one character in String, False otherwise.
S.isdigit()
- islower()
- It returns True if all cased characters in String are lowercase and there is at least one cased character in String, False otherwise.
S.islower()
- isspace()
- It returns True if all characters in String are whitespace and there is at least one character in String, False otherwise.
S.isspace()
- istitle()
- It returns True if String is a titlecased string and there is at least one character in String , i.e. upper- and titlecase characters may only follow uncased characters and lowercase characters only cased ones and returns False otherwise.
- It returns True if String is a titlecased string and there is at least one character in String , i.e. upper- and titlecase characters may only follow uncased characters and lowercase characters only cased ones and returns False otherwise.
- isupper()
- Return True if all cased characters in String are uppercase and there is at least one cased character in String , False otherwise
S.isupper()
- len()
- It returns the integer value of number of characters present in the string including whitespace character.
len(string)
- lower()
- It returns a copy of the string converted to lowercase.
S.lower()
- lstrip()
- It returns a copy of the string with leading whitespace removed.
- If chars is given and not None, remove characters in chars instead.
S.lstrip([chars])
- max()
- It returns the character with maximum number of Unicode (integer ordinal)
max(str)
- min()
- It returns minimum (integer ordinal) value character.
min(str)
- replace(old,new,max)
- It returns a copy of String with all occurrences of sub string old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced. replace(old,new,max)
- It returns a copy of String with all occurrences of sub string old replaced by new. If the optional argument count is
- rfind(str,start=0,end=len(string))
- It returns the highest index in String where sub string is found, such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation. - It returns – on faliure.
S.rfind(str,start=0,end=len(string))
- It returns the highest index in String where sub string is found, such that sub is contained within S[start:end]. Optional
- rindex(str,start=0,end=len(string))
- It is like S.rfind() but raises ValueError when the sub string is not found.
S.rindex(str,start=0,end=len(string))
- rstrip()
- It returns a copy of the string with trailing whitespace removed.
- If chars is given and not None, remove characters in chars instead.
S.rstrip([chars])
- startswith(prefix,start=0,end=len(string))
- It returns True if String starts with the specified prefix, False otherwise.
S.startswith(prefix,start=0,end=len(string))
- swapcase()
- It returns a copy of String with uppercase characters converted to lowercase and vice versa.
S.swapcase()
- title()
- It returns a titlecased version of String, i.e. words start with title case characters, all remaining cased characters have lower case.
S.title()
Example of string methods
#STRING METHODS IN PYTHON (itvoyagers.in)
s="itvoyagers welcome you"
print('s',s)
print("s.capitalize()",s.capitalize()) #returns capitalized string
print("s.count('e',0,22)",s.count('e',0,22))
print("s.endswith('s',0,22)",s.endswith('s',0,22))
print("s.endswith('u',0,22)",s.endswith('u',0,22))
print("s.find('O',0,22)",s.find('O',0,22)) #returns -1 if not found else index
print("s.find('e',0,22)",s.find('e',0,22)) #returns index if found
print("s.index('e',0,22)",s.index('e',0,22)) #returns index if found
#remove comment from below line to execute code
#print("s.index('E',0,22)",s.index('E',0,22)) #returns error if not found
st='5star'
print('st',st)
print("st.isalnum()",st.isalnum()) #returns True only if alphabets or numbers
st1='star'
print('st1',st1)
print("st1.isalpha()",st1.isalpha()) #returns True only if alphabets
st2='5'
print('st2',st2)
print("st2.isdigit()",st2.isdigit())
print("s.islower()",s.islower()) #returns True is string is in lower case
print("s.isupper()",s.isupper()) #returns True is string is in upper case
st3=' '
print('st3',st3)
print("st3.isspace()",st3.isspace()) #returns True if the string consist of only space characters
st4="Hello Python"
print('st4',st4)
print("st4.istitle()",st4.istitle()) #checks if string is in title case
print("len(s)",len(s))
print("s.lower()",s.lower())
print("s.upper()",s.upper())
st5="This is a pleasant day and it is warm weather"
print("st5",st5)
print("st5.replace('is','was',1)",st5.replace('is','was',1))
print("st5.replace(' is',' was',1)",st5.replace(' is',' was',1))
print("s.rfind('O',0,22)",s.rfind('O',0,22))
print("s.rfind('e',0,22)",s.rfind('e',0,22))
print("s.rindex('e',0,22)",s.rindex('e',0,22))
#remove comment from below line to execute code
#print("s.rindex('E',0,22)",s.rindex('E',0,22))
st6=" Python "
print("st6",st6)
print("st6.rstrip()",st6.rstrip()) #removes trailing whitespaces
print("st6.lstrip()",st6.lstrip()) #removes leading whitespaces
print("s.startswith('i',0,22)",s.startswith('i',0,22))
print("s.startswith('I',0,22)",s.startswith('I',0,22))
print("st5.title()",st5.title()) #returns title case
print("st5.swapcase()",st5.swapcase()) #swaps upper to lower and lower to upper case
Output of string methods in python

Other python related post
Other advance python related post
We are aiming to explain all concepts of Python in easiest terms as possible.

ITVoyagers
Author