In: Computer Science
7.15 Translate the following string method invocations to functions calls in namespace str:
(Python)
(a) ' error'. upper( )
(b) ' 2, 3, 4, 5'. split(',' )
(c) ' mississippi'.count('i' )
(d) ' bell'.replace('e' , 'a' )
(e) ' '. format(1, 2, 3)
Here is the code explained as comments for you:
' error'. upper( ) #The string error will be
converted to upper case, and that string
#will then be converted to
upper case. So, it will be ' ERROR'
' 2, 3, 4, 5'. split(',' ) #The string will be separated/split
based on commas.
#So, the list is now: [' 2',
' 3', ' 4', ' 5']
'mississippi'.count('i') #The number of 'i's in the string will be
counted.
#So, will return
4.
' bell'.replace('e' , 'a' ) #The character e is
replaced with the character a in the string bell.
#So, the result is ball
' '.format(1, 2, 3) #Will just leave a space.