Python String Method Overview
Python String Method Overview
str = 'abc123'
x = [Link]()
print(x) # True
isalnum() Method
[Link]()
str = 'abc-123'
x = [Link]()
print(x) # False
str = '*abc123?'
x = [Link]()
print(x) # False
str = ''
x = [Link]()
print(x) # False
isalpha() Method
[Link]()
• 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 = [Link]() S = 'abc xyz'
print(x) # True x = [Link]()
print(x) # False
S = '123'
x = [Link]() S = ''
print(x) # False x = [Link]()
print(x) # False
S = 'abc123'
x = [Link]()
print(x) # False
isdecimal() Method
[Link]()
• 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 = [Link]()
print(x) # True
isdecimal() Method
[Link]()
# floating point number
S = '123.456'
x = [Link]()
print(x) # False
S = '123$@%'
x = [Link]()
print(x) # False
S = 'A123$@%'
x = [Link]()
print(x) # True
S = 'ABCDe'
x = [Link]()
print(x) # False
isprintable() Method
[Link]()
• 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
[Link]()
# Check if all characters in the string are printable
S = 'Hello, World!'
x = [Link]()
print(x) # True
Medium Mathematical
U+205F Space
U+3000 Ideographic Space
isidentifier() Method
[Link]()
• 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([Link]()) # False
S = 'totalCount'
print([Link]()) # True
S = '123totalCount'
print([Link]()) # False
S = 'total_Count'
print([Link]()) # True
S = 'totalCount123'
print([Link]()) # True
S = 'total Count'
S = ''
print([Link]()) # False
print([Link]()) # False
istitle() Method
[Link]()
• 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([Link]()) # True print([Link]()) # False
count()
find()
index()
rfind()
rindex()
startswith()
endswith()
count() Method
[Link](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
[Link](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 = [Link]('World','Universe') x = [Link]('Long','Small', 2)
print(x) # Hello, Universe! print(x) # Small, Smaller, Longest
strip()
rstrip()
lstrip()
strip() Method
[Link](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 = [Link]()
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 = [Link]()
print(S)
print(x) Hello,World!
strip() Method
[Link](chars)
Strip Characters
• By adding chars parameter, we can also specify the
character we want to strip.
S = 'aaabaaaa'
x = [Link]('a')
print(x) #b
S = 'xxxxSxxxxSxxxx'
x = [Link]('x')
print(x) #SxxxxS
S = 'xxxxSxxxxSxxxx'
x = [Link]('x')
print(x) #SxxxxSxxxx
xxxxSxxxxS
S = 'xxxxSxxxxSxxxx'
x = [Link]('x')
print(x)
partition()
rpartition()
split()
splitness()
rsplit()
partition() Method
[Link](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 = [Link]('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 = [Link]('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 = [Link]('it')
print(x) # ('Do ', 'it', ' now and keep it simple')
rpartition() Method
[Link](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 = [Link]('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 = [Link]('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 = [Link]('it')
print(x) # ('Do it now and keep ', 'it', ' simple')
split() Method
[Link](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
[Link](delimiter,maxsplit)
text= 'Earth is beautiful'
# splits at space
print([Link]()) #['Earth', 'is', 'beautiful']
print(text) #Earth is beautiful
grocery = 'Milk,Bread,Butter,Jam'
# splits at ','
print([Link](',')) #['Milk', 'Bread', 'Butter', 'Jam']
# Splitting at ':'
print([Link](':')) #['Milk,Bread,Butter,Jam']
split() Method
[Link](delimiter,maxsplit)
grocery = 'Milk,Bread,Butter,Jam'
print([Link](',', 2)) #['Milk', 'Bread', 'Butter,Jam']
# maxsplit: 1
print([Link](',', 1)) #['Milk', 'Bread,Butter,Jam']
# maxsplit: 5
print([Link](',', 5)) #['Milk', 'Bread', 'Butter', 'Jam']
# maxsplit: 0
print([Link](',', 0)) #['Milk,Bread,Butter,Jam‘]
splitlines() Method
[Link](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 = [Link]()
print(x) # ['First line', 'Second line']
S = 'First\nSecond\r\nThird\fFourth'
x = [Link]()
print(x) # ['First', 'Second', 'Third', 'Fourth']
# rsplit()
S = 'The World is Beautiful'
x = [Link](None,1)
print(x) # ['The World is', 'Beautiful']
# split()
S = 'The World is Beautiful'
x = [Link](None,1)
print(x) # ['The', 'World is Beautiful']
Methods to concatenate multiple strings
join()
join() Method
[Link](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
[Link](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
[Link]()
• 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 = [Link]() x = [Link]()
print(x) # HELLO, WORLD! print(x) # 123 ABC $@%
lower() Method
[Link]()
• 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 = [Link]()
print(x) # hello, world!
S = 'hello, world!'
x = [Link]()
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 = [Link]()
x = [Link]()
print(x) # They'Re Babu'S Friends.
print(x) # C3Po Is A Droid
• As a workaround for this you can use [Link]()
import string import string
S = "c3po is a droid" S = "they're Babu's friends."
x = [Link](S) x = [Link](S)
print(x) # C3po Is A Droid print(x) # They're Babu's Friends.
[Link](s, sep=None): (It is a Helper functions)
This method split the argument into words using [Link](), capitalize each word
using [Link](), and join the capitalized words using [Link](). 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
[Link](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
[Link](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 = [Link]()
print(e) #b'Das stra\xc3\x9fe'
Encoding with error parameter
str = 'Das straße'
e = [Link](encoding='ascii',errors='backslashreplace')
print(e) # b'Das stra\\xdfe'
e = [Link](encoding='ascii',errors='ignore')
print(e) # b'Das strae'
e = [Link](encoding='ascii',errors='namereplace')
print(e) # b'Das stra\\N{LATIN SMALL LETTER SHARP S}e'
e = [Link](encoding='ascii',errors='replace')
print(e) # b'Das stra?e'
e = [Link](encoding='ascii',errors='xmlcharrefreplace')
print(e) # b'Das straße'
e = [Link](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 = [Link](12)
print(x) #Left
rjust() Method
[Link](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 = [Link](12) Right x = '{:>12}'.format(S)
print(x) Right print(x)
S = 'Right'
x = [Link](12, '*')
print(x) #*******Right
zfill() Method
[Link](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 = [Link](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 = [Link](6) x = [Link](6)
print(x) #+00042 print(x) #-00042
expandtabs() Method
[Link](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([Link](2))
print([Link]()) print([Link](4))
print([Link]()) print([Link](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()