Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Python String Methods

The document provides an overview of various Python string methods, including those for checking string contents (e.g., isalnum(), isalpha(), isdecimal()) and methods for searching within strings (e.g., count(), find(), index()). Each method is explained with its functionality, return values, and examples. It also highlights the differences between similar methods, such as find() and index().

Uploaded by

gj88rd466g
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python String Methods

The document provides an overview of various Python string methods, including those for checking string contents (e.g., isalnum(), isalpha(), isdecimal()) and methods for searching within strings (e.g., count(), find(), index()). Each method is explained with its functionality, return values, and examples. It also highlights the differences between similar methods, such as find() and index().

Uploaded by

gj88rd466g
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 88

Python String Methods

Methods to check the contents of a string


 isalnum()
 isalpha()
 isdecimal()
 isdigit()
 isidentifier()
 islower()
 isnumeric()
 isprintable()
 isspace()
 istitle()
 isupper()
isalnum() Method
string.isalnum()
• This method returns TRUE if the string is nonempty and all
characters in it are alphanumeric. Otherwise, it returns
FALSE.
• A character is alphanumeric if it is either a letter [a-z],[A-
Z] or a number[0-9].

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 = 'abc 123'


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

# number with thousands separator


S = '1,234,567'
x = S.isdecimal() Unicode character such as U+0660
print(x) # False (Arabic-Indic Digit Zero) is also
considered as a decimal.
# empty string S = '\u0660'
S = '' print(S)
x = S.isdecimal() x = S.isdecimal()
print(x) # False print(x) # True
isdigit() Method
string.isdigit()
• This method returns TRUE if the string is nonempty
and all characters in it are digits. Otherwise, it
returns FALSE.
• Unicode characters such as superscript
digits ¹ , ² and ³ are also considered as digits.
S = '123'
x = S.isdigit()
print(x) # True

# floating point number


S = '123.456'
x = S.isdigit()
print(x) # False
isdigit() Method
string.isdigit()
# number with thousands #Unicode character such as superscript
separator digit ² is considered as a digit.
S = '1,234,567' S='10\u00B2'
x = S.isdigit() print(S) #10²
print(x) # False x = S.isdigit()
# empty string print(x) # True
S = ''
x = S.isdigit() #Special Unicode characters like circled
print(x) # False digits ⑥ are also considered as digits.

S = '\u2465' # Special Unicode ⑥


print(S) # ⑥
x = S.isdigit() # True
isnumeric() Method
string.isnumeric()
• This method returns TRUE if the string is nonempty
and all characters in it are numeric characters.
Otherwise, it returns FALSE.
• Numeric characters include digit characters, and all
characters that have the Unicode numeric value
property.
• e.g. ² (U+00b2, Superscript Two), ⅕ (U+2155, Vulgar
Fraction One Fifth)
isnumeric() Method
string.isnumeric()
# empty string
S = '123' S = ''
x = S.isnumeric() x = S.isnumeric()
print(x) # True print(x) # False
# floating point number
S = '123.456' S = '\u00b2'
x = S.isnumeric() print(S) #²
print(x) # False x = S.isnumeric()
# number with thousands print(x) # True
separator
S = '1,234,567' S = '\u2153'
x = S.isnumeric() print(S) #⅓
print(x) # False x = S.isnumeric()
print(x) # True
isdecimal() vs isdigit() vs isnumeric()
• Is 42 a decimal or digit or numeric number?
print('42'.isdecimal()) # True
print('42'.isdigit()) # True
print('42'.isnumeric()) # True
isdecimal() vs isdigit() vs isnumeric()
• Is ² (Superscript Two) a decimal or digit or numeric
number?
print('\u00b2'.isdecimal()) # False
print('\u00b2'.isdigit()) # True
print('\u00b2'.isnumeric()) # True
• s ⅓ (Vulgar Fraction One Third) a decimal or digit or
numeric number?
print('\u2153'.isdecimal()) # False
print('\u2153'.isdigit()) # False
print('\u2153'.isnumeric()) # True
isdecimal() vs isdigit() vs isnumeric()
• isdecimal() method supports only Decimal
Numbers.
• isdigit() method supports Decimals,
Subscripts, Superscripts.
• isnumeric() method supports Digits, Vulgar
Fractions, Subscripts, Superscripts, Roman
Numerals, Currency Numerators.
islower() Method
string.islower()
• This method doesn't take any parameters.
• This method returns:
• True if all alphabets that exist in the string are
lowercase alphabets.
• False if the string contains at least one uppercase
alphabet.
S = 'abcd'
x = S.islower()
print(x) # True
islower() Method
string.islower()
#The string doesn’t contain at least one cased character.
S = '123$@%'
x = S.islower()
print(x) # False

#The string contain at least one cased character.


S = 'a123$@%'
x = S.islower()
print(x) # True

#The string contains at least one uppercase alphabet.


S = 'abcdE'
x = S.islower()
print(x) # False
isupper() Method
string.isupper()
• This method doesn't take any parameters.
• Thismethod returns:
– True if all characters in a string are uppercase
characters
– False if any characters in a string are lowercase
characters
S = 'ABCD'
x = S.isupper()
print(x) # True
isupper() Method
string.isupper()
#The string doesn’t contain at least one cased character.

S = '123$@%'
x = S.isupper()
print(x) # False

#The string contain at least one cased character.

S = 'A123$@%'
x = S.isupper()
print(x) # True

#The string contains at least one lowercase alphabet.

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

# Line feed \n and tab \t are nonprintable characters


S = '\tHello,\nWorld!'
x = S.isprintable()
print(x) # False

# Empty string is considered printable


S = ''
x = S.isprintable()
print(x) # True
isspace() Method
string.isspace()
• This method returns TRUE if the string is nonempty
and all characters in it are whitespace characters.
– Characters that are used for spacing are called whitespace
characters.
– For example: tabs (\t), spaces (‘ ‘), newline(\n), Form feed (\
f), carriage return (\r)
• Otherwise, it returns FALSE
S=' ' S = ' \t \n \r \f '
x = S.isspace() x = S.isspace()
print(x) # True print(x) # True
S = ' a'
x = S.isspace()
print(x) # False
Unicode Whitespace Characters
Unicode Character Description
U+0020 Space
U+00A0 No-Break Space
U+1680 Ogham Space Mark
U+2000 En Quad
U+2001 Em Quad
U+2002 En Space S = '\u2005 \u2007'
U+2003 Em Space x = S.isspace()
U+2004 Three-Per-Em Space print(x) # True
U+2005 Four-Per-Em Space
U+2006 Six-Per-Em Space
U+2007 Figure Space
U+2008 Punctuation Space
U+2009 Thin Space
U+200A Hair Space

U+202F Narrow No-Break Space

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

# numbers and characters # lowercase


S = '*** Hello, World! 123' S = 'hello, world!'
print(S.istitle()) # True print(S.istitle()) # False
# Empty string
S = ''
print(S.istitle()) # False
Methods to search the contents of a string

 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

str = 'welcome friends, welcome to MIT'


ct=str .count('welcome',7)
print("Count=",ct) #Count= 1

str = 'welcome friends, welcome to MIT'


ct=str .count('welcome',7,15)
print("Count=",ct) #Count= 0
find() Method
string.find(sub,start,end)
• This method searches for the first occurrence of the
specified substring sub and returns its index.
• If specified substring is not found, it returns -1.
• The optional arguments start and end are used to
limit the search to a particular portion of the string.
• This method should be used only if we need to know
the position of sub.
• To check if sub is a substring or not, use the in
operator:
>>> 'Py' in 'Python' #True
find() Method
string.find(sub,start,end)
str="Rohit is student at BVM"
i=str.find('student')
print(i) #10

str="Rohit is student at BVM"


i=str.find('employee')
print(i) #-1

str="let it be, let it be"


i=str.find('it',6)
print(i) #15

str="let it be, let it be"


i=str.find('it',2,14)
print(i) #4
index() Method
string.index(sub,start,end)

• This method searches for the first occurrence of the


specified substring sub and returns its index.
• If specified substring is not found, it raises
ValueError exception.
• The optional arguments start and end are used to limit
the search to a particular portion of the string.
index() Method
string.index(sub,start,end)
str="Rohit is student at BVM"
i=str.index('student')
print(i) #9

str="Rohit is student at BVM" Traceback (most recent call last):


i=str.index('employee') i=str.index('employee')
print(i) ValueError: substring not found

str="Rohit is student at BVM"


i=str.index('is',5,10)
print(i) #6

str="Rohit is student at BVM" Traceback (most recent call last):


i=str.index('is',10) i=str.index('is',10)
print(i) ValueError: substring not found
find() vs index()
• The find() method is identical to the index() method.
• The only difference is that the index() method raises
a ValueError exception, if the substring is not found.

str="Rohit is student at BVM" #-1


i=str.find('employee')
print(i)

str="Rohit is student at BVM" Traceback (most recent call last):


i=str.index('employee') …
i=str.index('employee')
print(i)
ValueError: substring not found
rfind() Method
string.rfind(sub,start,end)
• This method searches for the last occurrence of the
specified substring sub and returns its index.
• If specified substring is not found, it returns -1.
• The optional arguments start and end are used to limit
the search to a particular portion of the string.
S = 'Big, Bigger, Biggest' S = 'Big, Bigger, Biggest'
x = S.rfind('Big') x = S.rfind('Big',2,10)
print(x) # 13 print(x) # 5

S = 'Big, Bigger, Biggest'


x = S.rfind('Small')
print(x) # -1
rindex() Method
string.rindex(sub,start,end)
• This method searches for the last occurrence of the specified
substring sub and returns its index.
• If specified substring is not found, it
raises ValueError exception.
• The optional arguments start and end are used to limit the
search to a particular portion of the string.
S = 'Big, Bigger, Biggest' S = 'Big, Bigger, Biggest'
x = S.rindex('Big') x = S.rindex('Small')
print(x) # 13 print(x)
Traceback (most recent call last):
S = 'Big, Bigger, Biggest' …
x = S.rindex('Big',2,10) x = S.rindex('Small')
print(x) # 5 ValueError: substring not found
rfind() vs rindex()
• The rfind() method is identical to the rindex() method.
• The only difference is that the rindex() method raises
a ValueError exception, if the substring is not found.

S = 'Big, Bigger, Biggest' S = 'Big, Bigger, Biggest'


x = S.rfind('Small') x = S.rindex('Small')
print(x) # -1 print(x)
Traceback (most recent call last):

x = S.rindex('Small')
ValueError: substring not found
startswith() Method
string.startswith(prefix,start,end)
• This method returns True if the string starts with the
specified prefix, otherwise returns False.
• we can limit the search by specifying optional
arguments start and end.
• startswith() also accepts a tuple of prefixes to look for

str = 'Rohit is a student of BVM'


x = str.startswith('Rohit')
print(x) # True
startswith() Method
string.startswith(prefix,start,end)
str = 'Rohit is a student of BVM'
x = str.startswith('Rohit')
print(x) # True

str = 'Rohit is a student of BVM'


x = str.startswith('is')
print(x) # False

str = 'Rohit is a student of BVM'


x = str.startswith('is',7,15)
print(x) # True

str = 'Rohit is a student of BVM'


x = str.startswith(('Balu','Rohit','Devi'))
print(x) # True
endswith() Method
string.endswith(suffix,start,end)
• This method returns True if the string ends with the
specified suffix, otherwise returns False.
• we can limit the search by specifying optional
arguments start and end.
• This method also accepts a tuple of suffixes to look for.

str = 'Rohit is a student of BVM'


x = str.endswith('BVM')
print(x) # True
endswith() Method
string.endswith(suffix,start,end)
str = 'Rohit is a student of BVM'
x = str.endswith('is')
print(x) # False

To limit the search to the substring, specify the strat and


end parameters:
str = 'Rohit is a student of BVM'
x = str.endswith('is',2,9)
print(x) # True

Provide Multiple Suffixes to Look for


str = 'Rohit is a student of BVM'
x = str.endswith(('DAV','BVM','ZION'))
print(x) # True
Methods to replace the contents of a
string
(returns a copy of the string where old substring is
replaced with the new substring. The original string is
unchanged)

 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

S = 'Long, Longer, Longest'


x = S.replace('Long','Small')
print(x) # Small, Smaller, Smallest
Methods to remove the contents of a string
The original string is unchanged

 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

Strip Multiple Characters


S = 'www.example.com'
x = S.strip('cmowz.')
print(x) #example
More About strip() Method

• Characters are removed from both


ends until reaching a string character that is not
contained in the set of characters in chars.
strip() Method
string.strip(chars)

S = 'xxxxSxxxxSxxxx'
x = S.strip('x')
print(x) #SxxxxS

S = '... - Version 3.2 Model-32 ...'


x = S.strip('.- ')
print(x) #Version 3.2 Model-32
lstrip() Method
string.lstrip(chars)
• This method removes whitespace from the beginning
(leading) 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.lstrip()
print(S) Hello, World!
print(x) Hello, World!
• Newline '\n', tab '\t' and carriage return '\r' are also considered
as whitespace characters.
S = ' \t\n\r Hello, World! '
x = S.lstrip()
print(S) Hello, World!
print(x) Hello, World!
lstrip() Method
string.lstrip(chars)
Strip Characters
S = 'aaaaab'
x = S.lstrip('a')
print(S) #aaaaab
print(x) #b

Strip Multiple Characters


S = 'http://www.example.com'
x = S.lstrip('hwtp:/.')
print(x) #example.com
More About lstrip() Method

S = 'xxxxSxxxxSxxxx'
x = S.lstrip('x')
print(x) #SxxxxSxxxx

S = '... - Version 3.2 Model-32'


x = S.lstrip('.- ')
print(x) #Version 3.2 Model-32
rstrip() Method
string.rstrip(chars)
• This method removes whitespace from the right end (trailing) of
the string by default.
• By adding chars parameter, we can also specify the characters we
want to strip.
S = ' Hello, World! '
x = S.rstrip()
print(S) # Hello, World!
print(x) # Hello, World!

S = ' Hello, World! \t\n\r ' # Hello, World!


x = S.rstrip()
print(S)
print(x) # Hello, World!
rstrip() Method
string.rstrip(chars)
Strip Characters
• By adding chars parameter, we can also specify the
character we want to strip.
S = 'baaaaa'
x = S.rstrip('a')
print(x) #b

Strip Multiple Characters


S = 'example.com/wow'
x = S.rstrip('wo/')
print(x) #example.com
More About rstrip() Method

xxxxSxxxxS

S = 'xxxxSxxxxSxxxx'
x = S.rstrip('x')
print(x)

S = 'Version 3.2 Model-32 - ...'


x = S.rstrip('.- ')
print(x)
#Version 3.2 Model-32
Methods to split a string
The original string is unchanged

 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']

S = 'First line\nSecond line'


x = S.splitlines(True)
print(x) # ['First line\n', 'Second line']
rsplit() Method
string.rsplit(separator,maxsplit)
• This method takes maximum of 2 parameters:
– separator (optional)
• The is a delimiter.
• The method splits string starting from the right at the
specified separator.
• If the separator is not specified, any whitespace (space, newline etc.)
string is a separator.
– maxsplit (optional)
• This 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 starting
from the right and returns a list of strings.
rsplit() Method
string.rsplit(separator,maxsplit)
S = 'The World is Beautiful' #Limit Splits With Maxsplit
x = S.rsplit()
print(x) # ['The', 'World', 'is', 'Beautiful'] S = 'The World is Beautiful'
x = S.rsplit(None,1)
#Split on comma print(x) # ['The World is', 'Beautiful']
S = 'red,green,blue'
x = S.rsplit(',') S = 'The World is Beautiful'
print(x) # ['red', 'green', 'blue'] x = S.rsplit(None,2)
print(x) # ['The World', 'is', 'Beautiful']
#Delimiter with multiple characters
S = 'the beginning is the end is the beginning'
x = S.rsplit(' is ')
print(x) # ['the beginning', 'the end', 'the beginning']
rsplit() vs split()
• If maxsplit is specified, rsplit() counts splits from the
right end, whereas split() counts them from left.
• Otherwise, they both behave exactly the same.

# 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

#Join list items with newline


L = ['First Line', 'Second Line']
x = '\n'.join(L)
print(x)
# First Line
# Second Line

#Join with delimiter containing multiple characters


L = ['the beginning', 'the end', 'the beginning']
x = ' is '.join(L)
print(x) # the beginning is the end is the beginning
join() on Iterable of Size 1
• join() method is smart enough to insert the delimiter in
between the strings rather than just adding at the end of
every string.
• So, if we pass an iterable of size 1, we won’t see the
delimiter.

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

• To avoid such exception, we need to convert each item in a


list to string.
• The list comprehension makes this especially convenient.

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 = '123 ABC $@%'


x = S.lower()
print(x) # 123 abc $@%
casefold() Method
string.casefold()
• This method returns a casefolded (lowercase but
more aggressive) copy of the string.
• The method does not change the original string.
• Casefolded strings are usually used to ‘normalize‘
text for the purposes of caseless comparison
(especially when we want to take characters of many
different languages into account).
old_str = 'WELCOME to MIT!'
new_str = old_str.casefold()
print("New String:",new_str) #New String: welcome to mit!
print("Old String:",old_str) #Old String: WELCOME to MIT!
casefold() vs lower()
• Casefolding is similar to lowercasing but more aggressive
because it is intended to remove all case distinctions in a
string.
• For example, the German lowercase letter ‘ß‘ is equivalent
to ‘ss‘.
• Since it is already lowercase, lower() would do nothing to
‘ß‘, but casefold() converts it to ‘ss‘.
casefold() lower()
old_str = 'Das straße' old_str = 'Das straße'
new_str = old_str.casefold() new_str =old_str.lower()
print("Old String:",old_str) print("Old String:",old_str)
print("New String:",new_str) print("New String:",new_str)
Old String: Das straße Old String: Das straße
New String: das strasse New String: das straße
casefold() vs lower()
• If we are working strictly in the English language, lower
and casefold returns exactly the same results.
• However, if we are trying to normalize text from other
languages that use more than English 26-letter alphabet,
use casefold() to compare the strings for more consistent
results.
firstString = "der Fluß"
secondString = "der Fluss“
if firstString.casefold() == secondString.casefold():
print('The strings are equal.')
else:
print('The strings are not equal.')
#The strings are equal.
swapcase() Method
string.swapcase()
• This method doesn't take any parameters.
• This method returns the string where all uppercase
characters are converted to lowercase, and lowercase
characters are converted to uppercase.
S = 'Hello, World!'
x = S.swapcase()
print(x) # hELLO, wORLD!

#swapcase() method ignores numbers and special characters in a string.


S = '123 abc $@%'
x = S.swapcase()
print(x) # 123 ABC $@%
capitalize() Method
string.capitalize()
• This function doesn't take any parameter.
• This function returns a string with first letter capitalized and all
other characters lowercased.
• It doesn't modify the original string.
S = 'Rohit is A STUDENT.'
x = S.capitalize()
print(x) #Rohit is a student.
print(S) # Rohit is A STUDENT.

Non-alphabetic First Character:For the string with non-alphabetic first


character, the first character is kept unchanged while the rest is changed to
lowercase.
S = '19 is his ROLL number.'
x = S.capitalize()
print(x) #19 is his roll number.
print(S) #19 is his ROLL number.
title() Method
string.title()
• This method doesn't take any parameters.
• This method returns a copy of the string with first
letter of each word is converted to uppercase and
remaining letters are lowercase.
• The method does not change the original string.

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.

str = '{} is {} years old.'.format('Rohit', 13)


print(str) #Rohit is 13 years old.
center() Method
string.center(width,fillchar)
• This method returns center-aligned string of length width.
• Padding is done using the specified fillchar .
• The fillchar argument is optional. If it's not provided,
space is taken as default argument.
• The original string is returned as it is, if width is less than
or equal to string length.
Alternative Method
old_str = 'WELCOME' old_str = 'WELCOME'
new_str = old_str.center(15, '*') new_str = '{:*^15}'.format(old_str)
print("New String:",new_str) print("New String:",new_str)
print("Old String:",old_str) print("Old String:",old_str)
New String: ****WELCOME****
Old String: WELCOME
center() Method With Default fillchar

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

•This method doesn't modify the original string.


ljust() Method
string.ljust(width,fillchar)
• Thismethod 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 left-justified string within the given minimum width.
• If fillchar is defined, it also fills the remaining space with the defined
character.
Alternate Method
S = 'Left' S = 'Left'
x = S.ljust(12, '*') Output:
x = '{:*<12}'.format(S)
print(x) Left********
print(x)

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()

You might also like