Python String Methods
Python String Methods
str = 'abc123'
x = str.isalnum()
print(x) # True
isalnum() Method
string.isalnum()
str = 'abc-123'
x = str.isalnum()
print(x) # False
str = '*abc123?'
x = str.isalnum()
print(x) # False
str = ''
x = str.isalnum()
print(x) # False
isalpha() Method
string.isalpha()
• This method returns TRUE if the string is nonempty and all
characters in it are alphabetic (a-z or A-Z).
• Otherwise, it returns FALSE.
S = 'abc'
x = S.isalpha() S = 'abc xyz'
print(x) # True x = S.isalpha()
print(x) # False
S = '123'
x = S.isalpha() S = ''
print(x) # False x = S.isalpha()
print(x) # False
S = 'abc123'
x = S.isalpha()
print(x) # False
isdecimal() Method
string.isdecimal()
• This method returns TRUE if the string is nonempty
and all characters in it are decimal characters.
Otherwise, it returns FALSE.
• Decimal characters are those that can be used to form
numbers in base 10 (0-9).
• Unicode decimal character such as U+0660 (Arabic-
Indic Digit Zero) is also considered as a decimal.
S = '123'
x = S.isdecimal()
print(x) # True
isdecimal() Method
string.isdecimal()
# floating point number
S = '123.456'
x = S.isdecimal()
print(x) # False
S = '123$@%'
x = S.isupper()
print(x) # False
S = 'A123$@%'
x = S.isupper()
print(x) # True
S = 'ABCDe'
x = S.isupper()
print(x) # False
isprintable() Method
string.isprintable()
• This method returns TRUE if the string is empty or all characters in it
are printable.
• Characters that occupies printing space on the screen are known as
printable characters.
• For example:
• letters and symbols
• digits
• punctuation
• Whitespace
• It returns FALSE if the string contains at least one non-printable
character.
• Carriage return \r ,
• line feed \n
• tab \t are examples of nonprintable characters.
• A simple space character ' ' (0x20, ASCII space) is considered printable.
isprintable() Method
string.isprintable()
# Check if all characters in the string are printable
S = 'Hello, World!'
x = S.isprintable()
print(x) # True
Medium Mathematical
U+205F Space
U+3000 Ideographic Space
isidentifier() Method
string.isidentifier()
• This method returns TRUE if the string is a valid identifier
according to the language definition, and FALSE otherwise.
– A valid identifier can only have alphanumeric characters a-z, A-Z,
0-9 and underscore _ .
– The first character of an identifier cannot be a digit.
– Also, identifier should not match a Python keyword (reserved
identifier). S = 'total-Count'
print(S.isidentifier()) # False
S = 'totalCount'
print(S.isidentifier()) # True
S = '123totalCount'
print(S.isidentifier()) # False
S = 'total_Count'
print(S.isidentifier()) # True
S = 'totalCount123'
print(S.isidentifier()) # True
S = 'total Count'
S = ''
print(S.isidentifier()) # False
print(S.isidentifier()) # False
istitle() Method
string.istitle()
• This method returns TRUE if the string is nonempty and a
titlecased string. Otherwise, it returns FALSE.
• Numbers and special characters are ignored.
• In titlecased string each word starts with an uppercase
character and the remaining characters are lowercase.
# titlecase # uppercase
S = 'Hello World' S = 'HELLO, WORLD!'
print(S.istitle()) # True print(S.istitle()) # False
count()
find()
index()
rfind()
rindex()
startswith()
endswith()
count() Method
string.count(sub,start,end)
• This method searches the substring in the given string and returns
how many times the substring is present in it.
• It also takes optional parameters start and end to specify the
starting and ending positions in the string respectively.
str = 'welcome friends, welcome to MIT'
ct=str .count('welcome')
print("Count=",ct) #Count= 2
replace()
replace() Method
string.replace(old,new,count)
• This method returns a copy of string with all occurrences
of old substring replaced by new.
• By default, all occurrences of the substring are removed.
• However, we can limit the number of replacements by
specifying optional parameter count.
S = 'Hello, World!' S = 'Long, Longer, Longest'
x = S.replace('World','Universe') x = S.replace('Long','Small', 2)
print(x) # Hello, Universe! print(x) # Small, Smaller, Longest
strip()
rstrip()
lstrip()
strip() Method
string.strip(chars)
• This method removes whitespace from the beginning
(leading) and end (trailing) of the string by default.
• By adding chars(optional) parameter, we can also specify
the characters we want to strip.
S = ' Hello, World! '
x = S.strip()
print(S) Hello, World!
print(x) Hello, World!
#Newline '\n', tab '\t' and carriage return '\r' are also considered as whitespace
characters.
S = ' \t Hello,World!\n\r ' Hello,World!
x = S.strip()
print(S)
print(x) Hello,World!
strip() Method
string.strip(chars)
Strip Characters
• By adding chars parameter, we can also specify the
character we want to strip.
S = 'aaabaaaa'
x = S.strip('a')
print(x) #b
S = 'xxxxSxxxxSxxxx'
x = S.strip('x')
print(x) #SxxxxS
S = 'xxxxSxxxxSxxxx'
x = S.lstrip('x')
print(x) #SxxxxSxxxx
xxxxSxxxxS
S = 'xxxxSxxxxSxxxx'
x = S.rstrip('x')
print(x)
partition()
rpartition()
split()
splitness()
rsplit()
partition() Method
string.partition(separator)
• This method splits the string at the first occurrence of separator, and returns
a tuple containing three items.
– The part before the separator
– The separator itself
– The part after the separator
S = 'Do it now and keep it simple'
x = S.partition('and')
print(x) # ('Do it now ', 'and', ' keep it simple')
#No Match Found: If the separator is not found, the method returns a tuple
containing the string itself, followed by two empty strings.
S = 'Do it now and keep it simple'
x = S.partition('or')
print(x) # ('Do it now and keep it simple', '', '')
#Multiple Matches: If the separator is present multiple times, the method
splits the string at the first occurrence.
S = 'Do it now and keep it simple'
x = S.partition('it')
print(x) # ('Do ', 'it', ' now and keep it simple')
rpartition() Method
string.rpartition(separator)
• This method splits the string at the last occurrence of separator, and returns
a tuple containing three items.
– The part before the separator
– The separator itself
– The part after the separator
S = 'Do it now and keep it simple'
x = S.rpartition('and')
print(x) # ('Do it now ', 'and', ' keep it simple')
#No match found: If the separator is not found, the method returns a tuple
containing two empty strings, followed by the string itself.
S = 'Do it now and keep it simple'
x = S.rpartition('or')
print(x) # ('', '', 'Do it now and keep it simple')
#Multiple Matches : If the separator is present multiple times, the method splits
the string at the last occurrence.
S = 'Do it now and keep it simple'
x = S.rpartition('it')
print(x) # ('Do it now and keep ', 'it', ' simple')
split() Method
string.split(separator,maxsplit)
• This method takes maximum of 2 parameters:
– separator (optional)
• This is a delimiter
• The string splits at the specified separator.
• If the separator is not specified, any whitespace (space,
newline etc.) string is a separator.
– maxsplit (optional)
• The maxsplit defines the maximum number of splits
• The default value of maxsplit is -1, meaning, no limit on the
number of splits.
• This method breaks the string at the separator and
returns a list of strings.
split() Method
string.split(delimiter,maxsplit)
text= 'Earth is beautiful'
# splits at space
print(text.split()) #['Earth', 'is', 'beautiful']
print(text) #Earth is beautiful
grocery = 'Milk,Bread,Butter,Jam'
# splits at ','
print(grocery.split(',')) #['Milk', 'Bread', 'Butter', 'Jam']
# Splitting at ':'
print(grocery.split(':')) #['Milk,Bread,Butter,Jam']
split() Method
string.split(delimiter,maxsplit)
grocery = 'Milk,Bread,Butter,Jam'
print(grocery.split(',', 2)) #['Milk', 'Bread', 'Butter,Jam']
# maxsplit: 1
print(grocery.split(',', 1)) #['Milk', 'Bread,Butter,Jam']
# maxsplit: 5
print(grocery.split(',', 5)) #['Milk', 'Bread', 'Butter', 'Jam']
# maxsplit: 0
print(grocery.split(',', 0)) #['Milk,Bread,Butter,Jam‘]
splitlines() Method
string.splitlines(keepends)
• This method splits a string at line breaks and
returns them in a list.
• If the optional keepends argument is specified and
TRUE, line breaks are included in the resulting list.
S = 'First line\nSecond line'
x = S.splitlines()
print(x) # ['First line', 'Second line']
S = 'First\nSecond\r\nThird\fFourth'
x = S.splitlines()
print(x) # ['First', 'Second', 'Third', 'Fourth']
# rsplit()
S = 'The World is Beautiful'
x = S.rsplit(None,1)
print(x) # ['The World is', 'Beautiful']
# split()
S = 'The World is Beautiful'
x = S.split(None,1)
print(x) # ['The', 'World is Beautiful']
Methods to concatenate multiple strings
join()
join() Method
string.join(iterable)
• This method provides a flexible way to concatenate string.
• It concatenates each element of an iterable (such as list,
string and tuple whose items are strings) to the string and
returns the concatenated string.
• The join() method takes an iterable - objects capable of
returning its members one at a time
• If there are any non-string values in iterable, a TypeError
will be raised.
#Join all items in a list with comma
L = ['red', 'green', 'blue']
x = ','.join(L)
print(x) # red,green,blue
join() Method
string.join(iterable)
#Join list items with space
L = ['The', 'World', 'is', 'Beautiful']
x = ' '.join(L)
print(x) # The World is Beautiful
L = ['red']
x = ','.join(L)
print(x) # red
Join a List of Integers
Traceback (most recent call last):
L = [1, 2, 3, 4, 5, 6]
…
x = ','.join(L)
x = ','.join(L)
print(x)
TypeError: sequence item 0: expected str instance, int found
L = [1, 2, 3, 4, 5, 6]
x = ','.join(str(val) for val in L)
print(x) # 1,2,3,4,5,6
String modification methods
The original string is unchanged
upper()
lower()
casefold()
swapcase()
capitalize()
title()
encode()
upper() Method
string.upper()
• This method doesn't take any parameters.
• This method returns the uppercased string from the
given string.
• It converts all lowecase characters to uppercase.
• If no lowercase characters exist, it returns the original
string.
S = 'Hello, World!' S = '123 abc $@%'
x = S.upper() x = S.upper()
print(x) # HELLO, WORLD! print(x) # 123 ABC $@%
lower() Method
string.lower()
• This method doesn't take any parameters.
• This method returns the lowercased string from the given
string.
• It converts all uppercase characters to lowercase.
• If no uppercase characters exist, it returns the original string.
S = 'Hello, World!'
x = S.lower()
print(x) # hello, world!
S = 'hello, world!'
x = S.title()
print(x) # Hello, World!
Unexpected Behavior of title() Method
• The first letter after every number or special character
(such as Apostrophe) is converted into a upper case
letter.
S = "they're Babu's friends."
S = "c3po is a droid"
x = S.title()
x = S.title()
print(x) # They'Re Babu'S Friends.
print(x) # C3Po Is A Droid
• As a workaround for this you can use string.capwords()
import string import string
S = "c3po is a droid" S = "they're Babu's friends."
x = string.capwords(S) x = string.capwords(S)
print(x) # C3po Is A Droid print(x) # They're Babu's Friends.
string.capwords(s, sep=None): (It is a Helper functions)
This method split the argument into words using str.split(), capitalize each word
using str.capitalize(), and join the capitalized words using str.join(). If the optional
second argument sep is absent or None, runs of whitespace characters are replaced
by a single space and leading and trailing whitespace are removed, otherwise sep is
used to split and join the words.
encode() Method
string.encode(encoding='UTF-8',errors='strict')
• Since Python 3.0, strings are stored as Unicode, i.e. each
character in the string is represented by a code point.
• So, each string is just a sequence of Unicode code points.
• For efficient storage of these strings, the sequence of
code points are converted into set of bytes. The process
is known as encoding.
• There are various encodings present which treats a string
differently. The popular encodings being utf-8, ascii, etc.
• Using string's encode() method, we can convert
unicoded strings into any encodings supported by
Python. By default, Python uses utf-8 encoding.
encode() Method
string.encode(encoding='UTF-8',errors='strict')
• By default, encode() method doesn't require any parameters.
• It returns utf-8 encoded version of the string. In case of failure, it raises
a UnicodeDecodeError exception.
• However, it takes two parameters:
• encoding - the encoding type a string has to be encoded to
• errors - response when encoding fails. There are six types of error
response
strict - default response which raises a UnicodeDecodeError exception on failure
ignore - ignores the unencodable unicode from the result
replace - replaces the unencodable unicode to a question mark ?
xmlcharrefreplace - inserts XML character reference instead of unencodable
unicode
backslashreplace - inserts a \uNNNN espace sequence instead of unencodable
unicode
namereplace - inserts a \N{...} escape sequence instead of unencodable unicode
Encode the string to UTF-8:
str = 'Das straße'
e = str.encode()
print(e) #b'Das stra\xc3\x9fe'
Encoding with error parameter
str = 'Das straße'
e = str.encode(encoding='ascii',errors='backslashreplace')
print(e) # b'Das stra\\xdfe'
e = str.encode(encoding='ascii',errors='ignore')
print(e) # b'Das strae'
e = str.encode(encoding='ascii',errors='namereplace')
print(e) # b'Das stra\\N{LATIN SMALL LETTER SHARP S}e'
e = str.encode(encoding='ascii',errors='replace')
print(e) # b'Das stra?e'
e = str.encode(encoding='ascii',errors='xmlcharrefreplace')
print(e) # b'Das straße'
e = str.encode(encoding='UTF-8',errors='strict')
print(e) # b'Das stra\xc3\x9fe'
String formatting methods
format()
center()
ljust()
rjust()
zfill()
expandtabs()
format() Method
• Python also provides a more advanced and powerful
way to do string processing tasks – string formatting. It
allows us to embed variables inside a string.
old_str = 'WELCOME'
new_str = old_str.center(15)
print("New String:",new_str) New String: WELCOME
print("Old String:",old_str) Old String: WELCOME
S = 'Left'
x = S.ljust(12)
print(x) #Left
rjust() Method
string.rjust(width,fillchar)
• This method takes two parameters:
– width - width of the given string.
• If width is less than or equal to the length of the string, original string is returned.
– fillchar (Optional) - character to fill the remaining space of the width
• This method returns the right-justified string within the given
minimum width.
• If fillchar is defined, it fills the remaining space with the defined
S =character.
'Right' Output S = 'Right'
x = S.rjust(12) Right x = '{:>12}'.format(S)
print(x) Right print(x)
S = 'Right'
x = S.rjust(12, '*')
print(x) #*******Right
zfill() Method
string.zfill(width)
• This method returns a copy of string left padded with ‘0’
characters to make a string of length width.
• The original string is returned, if width is less than or equal
to string length. Alternate Method
S = '42' S = '42'
x = S.zfill(6) x = '{:0>6}'.format(S)
print(x) #000042 print(x) #000042
String with Sign Prefix
If the string contains a leading sign + or -, zeros are padded after the
sign character rather than before.
S = '+42' S = '-42'
x = S.zfill(6) x = S.zfill(6)
print(x) #+00042 print(x) #-00042
expandtabs() Method
string.expandtabs(tabsize)
• This method replaces each tab character ‘\t‘ in
a string with specified number of spaces (tabsize).
• The default tabsize is 8 (tab stop at every eighth
column). S = 'a\tb\tc'
S1 = 'a\tb\tc'
S2 = 'aaaa\tbbbb\tcccc' print(S.expandtabs(2))
print(S1.expandtabs()) print(S.expandtabs(4))
print(S2.expandtabs()) print(S.expandtabs(6))
abc
a b c a b c
aaaa bbbb cccc a b c
Common String Methods
• Checking the contents of a string:
– isalpha()
– isdigit()
– islower()
– isupper()
• Modifying the contents of a string:
– lower()
– upper()
• Searching the contents of a string:
– find(sub,start,end)
• Replacing the contents of a string
– replace(old,new,count)
• Removing the contents of a string
– strip()
• Splitting a string
– Split(seperator,maxsplit)
• Joining a string (concatenation)
– Join(iterable)
• Formatting a string
– format()