Starting from:
$30

$24

Sheet 06: Strings & Lists Solution

State whether true or false:-

A Python string literal is always enclosed in double quotes.
The last character of a strings is at position len( s ) - 1 .
A string always contains a single line of text.
In Python ‘4’ + “5” is “45”.
Python lists are mutable, but strings are not.
ASCII is a standard for representing characters using numeric codes.



The split method breaks a string into a list of substrings, and join does the opposite.



The add method can be used to add an item to the end of a list.



Multiple Choice




Accessing a single character out of a string is called:

slicing b) concatenation c) assignment d) indexing



Which of the following is the same ass [0 : -1] ?

s [- 1 ] b) s [ : ] c) s [ : len ( s ) - 1 ] d) s [0 : len ( s ) ]
What function gives the Unicode value of a character?

ord b) ascii c) chr d) eval
Which of the following cannot be used to convert a string of digits into a number?

int b) float c) str d) eval
A successor to ASCII that includes characters from (nearly) all written languages is

TELLI b) ASCII++ c) Unicode d) ISO
Which string method converts all the characters of a string to upper case?

capitalize b) capwords c) uppercase d) upper
The string "slots" that are filled in by the format method are marked by:

% b) $ c) [] d) {}
Which of the following is not a file-reading method in Python?

read b) readline c) readall d) readlines
The term for a program that does its input and output with files is

file-oriented b) multi-line c) batch d) lame
Before reading or writing to a file, a file object must be created via

open b) create c) File d) Folder



Discussion:




Given the initial statements: s 1 =" spam"



s2 = "ni ! "

Show the result of evaluating each of the following string expressions.




" The Knights who say , " + s2
3 * s 1 + 2 * s2
s 1 [1]
s 1 [ 1 : 3]



s 1 [2] + s2 [ : 2] f ) s 1 + s2 [ -1]



s 1 . upper ()
s2 . upper ( ) . ljust (4) * 3



Given the same initial statements as in the previous problem, show a Python expression that could construct each of the following results by performing string operations on s1 and s2.



"NI"
"ni ! spamni ! "
"Spam Ni ! Spam N1. .I Spam Ni ! "
" spam"
[ " sp " , "m" ]
" spm"



Show the output that would be generated by each of the following program fragments:



for ch in " aardvark" :
print (ch)




for w in " Now is the winter of our discontent . . . " . split ( ): print (w)



for w in "Mississippi " . split("i") :
print (w , end= " " )

msg = " "



for s in " secret ".split("e"): msg = msg + s

print (msg)

msg = " "
for ch in "secret" :




msg = msg + chr (ord ( ch) + 1 ) print (msg)




Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why.



" Looks l ike {1} and {0} for breakfast " . format ( " eggs " , " spam" )
" There is {0} {1} {2} {3}" . format ( ! , " spam" , 4 , " you" )
"Hello {0} " . format ( " Susan" , " Computewell " )
" {0 : 0 . 2f} {0 : 0 . 2f}" . format (2 . 3 , 2 . 3468)
" {7 . 5f} {7 . 5f} " . format (2 . 3 , 2 . 3468)
f ) "Time left {0 : 02} : {1 : 05 . 2f } " . format ( 1 , 37 . 374)

g) " {1 : 3} " . format ( " 14" )

Programming Exercises:




1- An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for "random access memory." Write a program that allows the user to type in a phrase and then outputs the acronym for that phrase. Note: The acronym should be all uppercase, even if the words in the phrase are not capitalized.




2- Numerologists claim to be able to determine a person's character traits based on the "numeric value" of a name. The value of a name is determined by summing up the values of the letters of the name where "a" is 1 "b" is 2 "c" is 3 up to "z" being 26 For example the name "Zelle" would have the value 26 + 5 + 12 + 12 + 5 = 60 (which happens to be a very auspicious number, by the way). Write a program that calculates the numeric value of a single name provided as input.




3- Write a program/function that counts the number of words in a sentence entered by the user.




4- Write a program/function that calculates the average word length in a sentence entered by the user.




------------------------------------------------------------------------------------------------------

More products