Sheet 06: Strings & Lists Solution

$30.00

Description

State whether true or false:-

  1. A Python string literal is always enclosed in double quotes.

  2. The last character of a strings is at position len( s ) – 1 .

  3. A string always contains a single line of text.

  4. In Python ‘4’ + “5” is “45”.

  5. Python lists are mutable, but strings are not.

  6. ASCII is a standard for representing characters using numeric codes.

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

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

Multiple Choice

  1. Accessing a single character out of a string is called:

    1. slicing b) concatenation c) assignment d) indexing

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

    1. s [- 1 ] b) s [ : ] c) s [ : len ( s ) – 1 ] d) s [0 : len ( s ) ]

  2. What function gives the Unicode value of a character?

    1. ord b) ascii c) chr d) eval

  3. Which of the following cannot be used to convert a string of digits into a number?

    1. int b) float c) str d) eval

  4. A successor to ASCII that includes characters from (nearly) all written languages is

    1. TELLI b) ASCII++ c) Unicode d) ISO

  5. Which string method converts all the characters of a string to upper case?

    1. capitalize b) capwords c) uppercase d) upper

  6. The string “slots” that are filled in by the format method are marked by:

    1. % b) $ c) [] d) {}

  7. Which of the following is not a file-reading method in Python?

    1. read b) readline c) readall d) readlines

  8. The term for a program that does its input and output with files is

    1. file-oriented b) multi-line c) batch d) lame

  9. Before reading or writing to a file, a file object must be created via

    1. open b) create c) File d) Folder

Discussion:

  1. Given the initial statements: s 1 =” spam”

s2 = “ni ! “

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

    1. ” The Knights who say , ” + s2

    2. 3 * s 1 + 2 * s2

    1. s 1 [1]

    2. s 1 [ 1 : 3]

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

    1. s 1 . upper ()

    2. s2 . upper ( ) . ljust (4) * 3

  1. 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.

    1. “NI”

    2. “ni ! spamni ! “

    3. “Spam Ni ! Spam N1. .I Spam Ni ! “

    4. ” spam”

    5. [ ” sp ” , “m” ]

    6. ” spm”

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

    1. for ch in ” aardvark” :

print (ch)

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

  1. for w in “Mississippi ” . split(“i”) :

print (w , end= ” ” )

    1. msg = ” “

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

print (msg)

    1. msg = ” “

for ch in “secret” :

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

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

    1. ” Looks l ike {1} and {0} for breakfast ” . format ( ” eggs ” , ” spam” )

    2. ” There is {0} {1} {2} {3}” . format ( ! , ” spam” , 4 , ” you” )

    3. “Hello {0} ” . format ( ” Susan” , ” Computewell ” )

    4. ” {0 : 0 . 2f} {0 : 0 . 2f}” . format (2 . 3 , 2 . 3468)

    5. ” {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.

——————————————————————————————————


error: Content is protected !!