Page 315 - Computer Science Class 11 With Functions
P. 315
-1
>>> colors.index('pink')
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
colors.index('pink')
ValueError: substring not found
● s.replace(old, new): The method replace(old, new) returns a string where all occurrences of the
string old are replaced by the string new. For instance, in the following function call, all the occurrences of 'Amey'
are replaced by 'Vihan' in the string, named message.
>>> message = 'Amey my friend, Amey my guide'
>>> message.replace('Amey', 'Vihan')
'Vihan my friend, Vihan my guide'
s.find(subStr): returns the index of the first occurrence of the string passed as an argument.
s.rfind(subStr): Returns the index of the last occurrence of a substring in the given string.
s.index(subStr): Returns the index of the first occurrence of the substring passed as an argument.
s.replace(old, new): Returns a string where all occurrences of the string old are replaced by the string new.
● s.split(delimit): The method split(delimit) returns a list of substrings, separated by the given
delimiter: delimit. The delimiter must be a string. When no delimiter is specified, Python splits the string on the
default delimiter, whitespace, i,e, any of the characters: ' ', '\n', '\t' . For example, consider the following
function calls where an explicit delimiter ',' is provided:
>>> addressBook = 'Name:Rahul,Address:ND Block Pitampura,PhoneNumber:9953799950'
>>> addressBook.split(',')
['Name:Rahul', 'Address:ND Block Pitampura', 'PhoneNumber:9953799950']
>>> friends = 'Aparajita, Animesh,, Aasrita, Pritam'
>>> friends.split(',')
['Aparajita', ' Animesh', '', ' Aasrita', ' Pritam']
Note that when the string friends is split, spaces after the delimiter become part of the next string in the resultant
list. Furthermore, note that the second and third comma appear in the string friends adjacent to each other. So, a
null string has also been included in the list. The following example is even more interesting,
>>> 'Awesome!!!, oh! my God!'.split('!')
['Awesome', '', '', ', oh', 'my God', '']
Next, let us split a multi-line string, using the default delimiter whitespace.
>>> address = '''Ram Niwas Yadav,
24/7, Ahmedpur, Vidisha,
PIN 464001'''
>>> address.split()
['Ram', 'Niwas', 'Yadav,', '24/7,', 'Ahmedpur,', 'Vidisha,', 'PIN', '464001']
● s.partitition(delimit): The method partition(delimit) splits the string into three parts, part of
the string before the delimiter, the delimiter itself, and the part of the string after the delimiter, as illustrated below:
>>> txt = 'Amey my friend, Amey my guide'
>>> txt.partition(',')
('Amey my friend', ',', 'Amey my guide')
s.split(delimit): Returns a list of substrings, separated by the given delimiter: delimit
s.partition(delimit): Splits the string into three parts, part of the string before the delimiter, the delimiter
itself, and the part of the string after the delimiter
s.join(list): Joins the strings in the given list (comma-separated sequence of values provided within square
brackets) together using the string s as the delimiter.
Strings 313

