Built-in String Methods
ASCII Value of Character
Example
c = 'A'
print("The ASCII value of '" + c + "' is", ord(c))
Character value of ASCII code
Example:
c=65
print("The character value of ",c," is ",chr(c))
Python String capitalize() method
It returns a copy of the string with only its first character capitalized.
Syntax:
str.capitalize()
Parameter:
NA
Return Value:
string
Example:
str = "this is string example....wow!!!";
print ("str.capitalize() : ", str.capitalize())
Python String lower() Method
Python string method lower() returns a copy of the string in which all case-based characters
have been lowercased.
Syntax
str.lower()
Parameters
NA
Return Value
This method returns a copy of the string in which all case-based characters have been
lowercased.
Example
str = "THIS IS STRING EXAMPLE....WOW!!!";
print(str.lower())
Python String upper() Method
Python string method upper() returns a copy of the string in which all case-based characters
have been uppercased.
Syntax
str.upper()
Parameters
NA
Return Value
This method returns a copy of the string in which all case-based characters have been
uppercased.
Example
str = "this is string example....wow!!!"
print ("str.capitalize() : ", str.upper())
Python String title() Method
Python string method title() returns a copy of the string in which first characters of all the words
are capitalized.
Syntax
str.title();
Parameters
NA
Return Value
This method returns a copy of the string in which first characters of all the words are
capitalized.
Example
str = "this is string example....wow!!!"
print (str.title())
Python String swapcase() Method
Python string method swapcase() returns a copy of the string in which all the case-based
characters have had their case swapped.
Syntax
str.swapcase();
Parameters
NA
Return Value
This method returns a copy of the string in which all the case-based characters have had
their case swapped.
Example
str = "this is string example....wow!!!"
print (str.swapcase())
str = "THIS IS STRING EXAMPLE....WOW!!!"
print (str.swapcase())
Python String islower() Method
Python string method islower() checks whether all the case-based characters (letters) of the
string are lowercase.
Syntax
str.islower()
Parameters
NA
Return Value
This method returns true if all cased characters in the string are lowercase and there is
at least one cased character, false otherwise.
Example
str = "THIS is string example....wow!!!";
print(str.islower())
str = "this is string example....wow!!!";
print(str.islower())
Python String isupper() Method
Python string method isupper() checks whether all the case-based characters (letters) of the
string are uppercase.
Syntax
str.isupper()
Parameters
NA
Return Value
This method returns true if all cased characters in the string are uppercase and there is
at least one cased character, false otherwise.
Example
str = "THIS IS STRING EXAMPLE....WOW!!!";
printstr.isupper()
str = "THIS is string example....wow!!!";
printstr.isupper()
Python String istitle() Method
Python string method istitle() checks whether all the case-based characters in the string
following non-casebased letters are uppercase and all other case-based characters are
lowercase.
Syntax
str.istitle()
Parameters
NA
Return Value
This method returns true if the string is a titlecased string and there is at least one
character, for example uppercase characters may only follow uncased characters and
lowercase characters only cased ones.It returns false otherwise.
Example
str = "This Is String Example...Wow!!!";
printstr.istitle()
str = "This is string example....wow!!!";
printstr.istitle()
Python String max() Method
Python string method max() returns the max alphabetical character from the string str.
Syntax
max(str)
Parameters
str − This is the string from which max alphabetical character needs to be returned.
Return Value
This method returns the max alphabetical character from the string str.
Example
str = "this is really a string example....wow!!!";
print("Max character: " + max(str))
str = "this is a string example....wow!!!";
print("Max character: " + max(str))
Python String min() Method
Python string method min() returns the min alphabetical character from the string str.
Syntax
min(str)
Parameters
str − This is the string from which min alphabetical character needs to be returned.
Return Value
This method returns the min alphabetical character from the string str.
Example
str = "this-is-real-string-example....wow!!!";
print ("Min character: " + min(str))
str = "this-is-a-string-example....wow!!!";
print ("Min character: " + min(str))
Python String len() Method
Python string method len() returns the length of the string.
Syntax
len(str )
Parameters
NA
Return Value
This method returns the length of the string.
Example
str = "this is string example....wow!!!";
print("Length of the string: ", len(str))
Python String replace() Method
Python string method replace() returns a copy of the string in which the occurrences of old have
been replaced with new, optionally restricting the number of replacements to max.
Syntax
str.replace(old, new[, max])
Parameters
old − This is old substring to be replaced.
new − This is new substring, which would replace old substring.
max − If this optional argument max is given, only the first count occurrences are
replaced.
Return Value
This method returns a copy of the string with all occurrences of substring old replaced
by new. If the optional argument max is given, only the first count occurrences are
replaced.
Example
str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 3))
Python String index() Method
Python string method index() determines if string str occurs in string or in a substring of string if
index beg and ending index end are given. This method is same as find(), but raises an exception
if sub is not found.
Syntax
str.index(str, beg = 0 end = len(string))
Parameters
str − This specifies the string to be searched.
beg − This is the starting index, by default its 0.
end − This is the ending index, by default its equal to the length of the string.
Return Value
Index if found otherwise raises an exception if str is not found.
Example
str1 = "this is string example....wow!!!";
str2 = "exam";
print(str1.index(str2))
print(str1.index(str2, 10))
print(str1.index(str2, 40))
Python String rindex() Method
Python string method rindex() returns the last index where the substring str is found, or raises
an exception if no such index exists, optionally restricting the search to string[beg:end].
Syntax
str.rindex(str, beg=0 end=len(string))
Parameters
str − This specifies the string to be searched.
beg − This is the starting index, by default its 0
len − This is ending index, by default its equal to the length of the string.
Return Value
This method returns last index if found otherwise raises an exception if str is not found.
Example
str1 = "this is string example....wow!!!"
str2 = "is"
print (str1.rindex(str2))
print (str1.index(str2))
Python String find() Method
Python string method find() determines if string str occurs in string, or in a substring of string if
starting index beg and ending index end are given.
Syntax
str.find(str, beg=0, end=len(string))
Parameters
str − This specifies the string to be searched.
beg − This is the starting index, by default its 0.
end − This is the ending index, by default its equal to the length of the string.
Return Value
Index if found and -1 otherwise.
Example
str1 = "this is string example....wow!!!"
str2 = "exam"
print (str1.find(str2))
print (str1.find(str2, 10))
print (str1.find(str2, 40))
Python String rfind() Method
Python string method rfind() returns the last index where the substring str is found, or -1 if no
such index exists, optionally restricting the search to string[beg:end].
Syntax
obj.rfind(str, beg=0 end=len(string))
Parameters
str − This specifies the string to be searched.
beg − This is the starting index, by default its 0.
end − This is the ending index, by default its equal to the length of the string.
Return Value
This method returns last index if found and -1 otherwise.
Example
str1 = "this is really a string example....wow!!!";
str2 = "is";
print (str1.rfind(str2))
print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))
print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))
Python String count() Method
Python string method count() returns the number of occurrences of substring sub in the range
[start, end]. Optional arguments start and end are interpreted as in slice notation.
Syntax:
str.count(sub, start= 0,end=len(string))
Parameters:
sub − This is the substring to be searched.
start − Search starts from this index. First character starts from 0 index. By default search starts
from 0 index.
end − Search ends from this index. First character starts from 0 index. By default search ends at
the last index.
Return Value:
Centered in a string of length width.
Example:
str = "this is string example....wow!!!";
sub = "i";
print("str.count(sub, 4, 40) : ", str.count(sub, 4, 40))
sub = "wow";
print("str.count(sub) : ", str.count(sub))
Python String endswith() Method
Python string method endswith() returns True if the string ends with the specified suffix,
otherwise return False optionally restricting the matching with the given indices start and end.
Syntax:
str.endswith(suffix,start,end)
Parameters:
suffix − This could be a string or could also be a tuple of suffixes to look for.
start − The slice begins from here.
end − The slice ends here.
Return Value:
TRUE if the string ends with the specified suffix, otherwise FALSE.
Example:
str = "this is string example....wow!!!";
suffix = "wow!!!";
printstr.endswith(suffix)
printstr.endswith(suffix,20)
suffix = "is";
printstr.endswith(suffix, 2, 4)
printstr.endswith(suffix, 3, 6)
Python String startswith() Method
Python string method startswith() checks whether string starts with str, optionally restricting the
matching with the given indices start and end.
Syntax
str.startswith(str, beg=0,end=len(string));
Parameters
str − This is the string to be checked.
beg − This is the optional parameter to set start index of the matching boundary.
end − This is the optional parameter to end start index of the matching boundary.
Return Value
This method returns true if found matching string otherwise false.
Example
str = "this is string example....wow!!!"
print (str.startswith( 'this' ))
print (str.startswith( 'is', 2, 4))
print (str.startswith( 'this', 2, 4 ))
Python String expandtabs() Method
Python string method expandtabs() returns a copy of the string in which tab characters ie. '\t'
are expanded using spaces, optionally using the given tabsize (default 8)..
Syntax
str.expandtabs(tabsize=8)
Parameters
tabsize − This specifies the number of characters to be replaced for a tab character '\t'.
Return Value
This method returns a copy of the string in which tab characters i.e., '\t' have been
expanded using spaces.
Example
str = "xyz\t12345\tabc"
print('Original String:', str)
print(str.expandtabs())
# tabsize is set to 2
print('Tabsize 2:', str.expandtabs(2))
# tabsize is set to 3
print('Tabsize 3:', str.expandtabs(3))
# tabsize is set to 4
print('Tabsize 4:', str.expandtabs(4))
# tabsize is set to 5
print('Tabsize 5:', str.expandtabs(5))
# tabsize is set to 6
print('Tabsize 6:', str.expandtabs(8))
Python String center() Method
Python string method center() returns centered in a string of length width. Padding is done
using the specified fillchar. Default filler is a space.
Syntax:
str.center(width[, fillchar])
Parameters:
width − This is the total width of the string.
fillchar − This is the filler character.
Return Value:
This method returns centered in a string of length width.
Example:
str = "this is string example....wow!!!"
print("str.center(40, 'a') : ", str.center(40, 'a'))
Python String ljust() Method
Python string method ljust() returns the string left justified in a string of length width. Padding is
done using the specified fillchar (default is a space). The original string is returned if width is less
than len(s).
Syntax
str.ljust(width[, fillchar])
Parameters
width − This is string length in total after padding.
fillchar − This is filler character, default is a space.
Return Value
This method returns the string left justified in a string of length width. Padding is done
using the specified fillchar (default is a space). The original string is returned if width is
less than len(s).
Example
str = "this is string example....wow!!!";
print(str.ljust(50, '0'))
Python String rjust() Method
Python string method rjust() returns the string right justified in a string of length width. Padding
is done using the specified fillchar (default is a space). The original string is returned if width is
less than len(s).
Syntax
str.rjust(width[, fillchar])
Parameters
width − This is the string length in total after padding.
fillchar − This is the filler character, default is a space.
Return Value
This method returns the string right justified in a string of length width. Padding is done
using the specified fillchar (default is a space). The original string is returned if width is
less than len(s).
Example
str= "this is string example....wow!!!"
print (str.rjust(50, '0'))
Python String zfill() Method
Python string method zfill() pads string on the left with zeros to fill width.
Syntax
str.zfill(width)
Parameters
width − This is final width of the string. This is the width which we would get after filling
zeros.
Return Value
This method returns padded string.
Example
str = "this is string example....wow!!!"
print (str.zfill(40))
print (str.zfill(50))
Python String strip() Method
Python string method strip() returns a copy of the string in which all chars have been stripped
from the beginning and the end of the string (default whitespace characters).
Syntax
str.strip([chars]);
Parameters
chars − The characters to be removed from beginning or end of the string.
Return Value
This method returns a copy of the string in which all chars have been stripped from the
beginning and the end of the string.
Example
str = "0000000this is string example....wow!!!0000000"
print (str.strip( '0' ))
Python String lstrip() Method
Python string method lstrip() returns a copy of the string in which all chars have been stripped
from the beginning of the string (default whitespace characters).
Syntax
str.lstrip([chars])
Parameters
chars − You can supply what chars have to be trimmed.
Return Value
This method returns a copy of the string in which all chars have been stripped from the
beginning of the string (default whitespace characters).
Example
str = " this is string example....wow!!! ";
print(str.lstrip())
str = "88888888this is string example....wow!!!8888888";
print(str.lstrip('8'))
Python String rstrip() Method
Python string method rstrip() returns a copy of the string in which all chars have been stripped
from the end of the string (default whitespace characters).
Syntax
str.rstrip([chars])
Parameters
chars − You can supply what chars have to be trimmed.
Return Value
This method returns a copy of the string in which all chars have been stripped from the
end of the string (default whitespace characters).
Example
str = " this is string example....wow!!! ";
print (str.rstrip())
str = "88888888this is string example....wow!!!8888888";
print (str.rstrip('8'))
Python String isspace() Method
Python string method isspace() checks whether the string consists of whitespace.
Syntax
str.isspace()
Parameters
NA
Return Value
This method returns true if there are only whitespace characters in the string and there
is at least one character, false otherwise.
Example
str = " ";
print(str.isspace())
str = "This is string example....wow!!!";
print(str.isspace())
Python String split() Method
Python string method split() returns a list of all the words in the string, using str as the separator
(splits on all whitespace if left unspecified), optionally limiting the number of splits to num.
Syntax
str.split(str="", num=string.count(str)).
Parameters
str − This is any delimeter, by default it is space.
num − this is number of lines minus one
Return Value
This method returns a list of lines.
Example
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print(str)
print (str.split( ))
print (str.split(' '),1)
Python String splitlines() Method
Python string method splitlines() returns a list with all the lines in string, optionally including the
line breaks (if num is supplied and is true)
Syntax
str.splitlines()
Parameters
Keepends − This is an optional parameter, if its value as true, line breaks need are also
included in the output.
Example 1
str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d";
print(str)
print (str.splitlines( ))
Example 2
str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"
print(str)
print (str.splitlines(True))
Python String join() Method
Python string method join() returns a string in which the string elements of sequence have been
joined by str separator.
Syntax
str.join(sequence)
Parameters
sequence − This is a sequence of the elements to be joined.
Return Value
This method returns a string, which is the concatenation of the strings in the sequence
seq. The separator between elements is the string providing this method.
Example
sep= "-";
seq = ("a", "b", "c") # This is sequence of strings.
Print(sep.join( seq ))
Python String isalpha() Method
Python string method isalpha() checks whether the string consists of alphabetic characters only.
Syntax
str.isalpha()
Parameters
NA
Return Value
This method returns true if all characters in the string are alphabetic and there is at least
one character, false otherwise.
Example
str = "this"; # No space & digit in this string
print(str.isalpha())
str = "this is string example....wow!!!";
print(str.isalpha())
Python String isalnum() Method
Python string method isalnum() checks whether the string consists of alphanumeric characters.
Syntax
str.isalnum()
Parameters
NA
Return Value
This method returns true if all characters in the string are alphanumeric and there is at
least one character, false otherwise.
Example
str = "this2009"; # No space in this string
print(str.isalnum())
str = "this is string example....wow!!!";
print(str.isalnum())
Python String isdecimal() Method
Python string method isdecimal() checks whether the string consists of only decimal characters.
This method are present only on unicode objects.
Only numbers and Unicode values for numbers are true
Note − To define a string as Unicode, one simply prefixes a 'u' to the opening quotation mark of
the assignment. Below is the example.
Syntax
str.isdecimal()
Parameters
NA
Return Value
This method returns true if all characters in the string are decimal, false otherwise.
Example
str = "this2009";
print(str.isdecimal())
str = "23443434"
print (str.isdecimal())
str=”\u0034” #”\u0034” is Unicode for 4
print(str.isdecimal())
Python String isdigit() Method
Python string method isdigit() checks whether the string consists of digits only.
Isdigit() supports the string consists of number,Unicode for number, superscripts(“\u00b3”) ,
subscript(“\u2082”)
Syntax
str.isdigit()
Parameters
NA
Return Value
This method returns true if all characters in the string are digits and there is at least one
character, false otherwise.
Example
str = "123456"; # Only digit in this string
print(str.isdigit())
str = "this is string example....wow!!!";
print(str.isdigit())
str=”\u2082” #subscript
print(str.isdigit())
str=”\u00b3” #superscript
print(str.isdigit())
Python String isnumeric() Method
string method isnumeric() checks whether the string consists of only numeric characters. This
method is present only on unicode objects.
isnumeric() supports the string consists of number,Unicode for numbers, superscripts(“\u00b3”)
, subscript(“\u2082”),romannumerals(“\u2161)
Syntax
str.isnumeric()
Parameters
NA
Return Value
This method returns true if all characters in the string are numeric, false otherwise.
Example
str = u"this2009";
print(str.isnumeric())
str = u"23443434";
print(str.isnumeric())
str='\u00b3' #superscript
print(str.isnumeric())
str='\u2083' #subscript
print(str.isnumeric())
s='\u2161' #romannumerals
print(str.isnumeric())