st fns
st fns
String Functions
• Python has a number of string functions which are in the string library.
• These functions are already built into every string - we invoke them by
appending the function to the string variable.
• These functions do not modify the original string, instead they return a
new string that has been altered.
Length function
• The len() function is used to find the length of a string including
spaces.
>>> word=‘hello world’
>>>len(word)
11
>>>word=‘\new text’
>>>len(word)
8
>>>word=r‘\new text’
>>>len(word)
9
Count function
• The count() function is used to find number of times a character or
word is repeated in a string.
>>> s='hello world'
>>> s.count('l')
3
>>> s='hi hi hi hello'
>>> s.count(' ')
3
>>> s.count('hi')
3
Find function
• The find() function is used to find the given character in a string. It
returns the position of the character if it finds.
>>> s='hello world'
>>> s.find('e')
1
>>> s.find(' ')
5
>>> s.find('hello')
0
>>> s1='hi hi'
>>> s1.find(' hi')
2
>>> s1.find('Hi')
-1
Index function
• The index() function is similar to find as it is used to find the given
character/word in a string. It returns the position of the character if it
finds. The index() function allows us to use index values to find out the
specified character or word in a string.
>>> s='hi Hello hi Hello' >>> s.index('hi')
>>> s.index('i') 0
1 >>> s.index('hi',5)
>>> s.index('H',0,5) 9
3 >>> s.index('l',8,-2)
>>> s.index('H',8) 14
12
Replace function
• The replace() function replaces a search character/word with specified
character or word. The syntax is
Parameter Description
old value Required. The string to search for
new value Required. The string to replace the old value with
count Optional. A number specifying how many occurrences of the old
value you want to replace. Default is all occurrences
Replace function
>>> s='hi hi hi hi'
>>> s.replace('hi', 'hello')
'hello hello hello hello'
>>> s
'hi hi hi hi'
>>> s.replace('hi','hello',1)
'hello hi hi hi'
>>> s.replace('hi','hello',2)
'hello hello hi hi'
>>> s.replace('Hi','Hello')
'hi hi hi hi'
Split function
• The split() function breaks the string based on the separator and
returns a list of strings where each string is a list item. The syntax is
Parameter Description
separator Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which
is "all occurrences"
Split function
>>> s='My name is srinivas'
>>> s.split()
['My', 'name', 'is', 'srinivas']
>>> s.split('i',1)
['My name ', 's srinivas']
>>> s.split('i',2)
['My name ', 's sr', 'nivas']
>>> s1='hi#hello#hi#hello'
>>> s1.split('#')
['hi', 'hello', 'hi', 'hello']
Capitalize function
• The capitalize() function converts the first character of a string to
capital (uppercase) letter and lowercases all other characters. If the
string has its first character as capital, then it returns the original
string. The syntax is
string name.capitalize()
The capitalize function does not take any parameters and returns a
string with the first character capitalized and all other characters lower
cased. It doesn’t modify the original string.
Capitalize function
>>> s='python Programming‘
>>> s.capitalize()
'Python programming‘
string name.lower()
string name.upper()
string name.title()
The title() function does not take any parameters. If the word contains
a number or a symbol, the first letter after that will be converted to
upper case.
>>> s='this is title case' >>> s='It is 2nd number'
>>> s.title() >>> s.title()
'This Is Title Case' 'It Is 2Nd Number'
Strip function
• The strip() function removes characters from both left and right based
on the argument (a string specifying the set of characters to be
removed) and returns a copy of the string by removing both the leading
and the trailing characters. The syntax is
string name.strip(characters)
string name.lstrip(characters)
string name.rstrip(characters)
string.join(iterable)
>>> '-'.join(s)
'one-two-three‘
>>> s1='Python'
>>> sep='-‘
>>> sep.join(s1)
'P-y-t-h-o-n‘
width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Ljust function
>>> s='Python Programming'
>>> s.ljust(50,'*')
'Python Programming********************************'
>>> s.ljust(100,'-')
'Python Programming-----------------------------------------------------------
-----------------------'
>>> s.ljust()
Traceback (most recent call last):
File "<pyshell#125>", line 1, in <module>
s.ljust()
TypeError: ljust expected at least 1 argument, got 0
>>> s.ljust(10)
'Python Programming'
Rjust function
• The rjust() function will right align the string i.e. it returns a right-
justified string of a given minimum width. The syntax is
width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Rjust function
>>> s='Python Programming‘
>>> s.rjust(50)
' Python Programming‘
>>> s.rjust(50,'*')
'********************************Python Programming‘
>>> s.rjust(10,'*')
'Python Programming'
Center function
• The center() function will center align the string i.e. it returns a center-
justified string of a given minimum width. The syntax is
width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Center function
>>> s='Python Programming‘
>>> s.center(50)
' Python Programming '
>>> s.center(50,'*')
'****************Python Programming****************'
>>> s.center(10,'*')
'Python Programming'