8.2. Exercises#

8.2.1. Exercise 1#

What would be the output of the following code?

'{age:d}, {name}, {name}'.format(age=28, name='John')
# write your answer here as a comment
# write your code here to check your understanding

8.2.2. Exercise 2#

What would the following code display?

'{0:d}, {1}, {1}'.format('John', 28)
# write your answer here as a comment
# write your code here to check your understanding

8.2.3. Exercise 3#

Create the following string and print it:

------------------------
Welcome to Food Science!
------------------------
# write your code here

8.2.4. Exercise 4#

What will be the output of the following statements:

  • 'apple'.replace('pp', 'nk')

  • 'orange'[3:]

  • 'pear'.replace('p', 'b').title()

# write your answers / code here

8.2.5. Exercise 5#

What will be the output of the following statements:

  • 'elc ' in 'Welcome to Food Science!'

  • 'welcome' in 'Welcome to Food Science!'

# write your answers / code here

8.2.6. Exercise 6#

Given the text below, build a dictionary to map a word to the number of times it appears in the text. Then print only the characters that appear more than once. Also remove any empty character. You have to use the splitline() method. NOTE: This is the same exercise as Exercise 6 in Sets and Dictionaries but here you are required to use splitline() to solve it.

text = """To be, or not to be, that is the question: 
Whether tis nobler in the mind to suffer 
The slings and arrows of outrageous fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep
No more; and by a sleep, to say we end."""
# write your code here