Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Exercise 5.1

  1. Assign the string "The Beagle" to a string variable called darwin_ship.

  2. Print darwin_ship.

  3. Print the length of darwin_ship.

darwin_ship = 'The Beagle' print( darwin_ship ) print( len(darwin_ship) )

Exercise 5.2

  1. Assign the empty string to the variable famous_scientists.

  2. Add "Jane" to your empty string.

  3. Add a space to your string.

  4. Add "Goodall" to your string and print it.

famous_scientists = '' famous_scientists += 'Jane' famous_scientists += ' ' famous_scientists += 'Goodall' print( famous_scientists )
Jane Goodall

Exercise 5.3

  1. Ask for input of your name.

  2. Ask for input of your age.

  3. Using an f-string print

    X is Y years old

where X is your name and Y is your age.

name = input('Enter your name: ') age = input('Enter your age: ') print( f'{name} is {age} years old' )

Exercise 5.4

  1. Ask for input of 2 decimal numbers.

  2. Using an f-string print

    X / Y = Z

where X and Y are the two numbers and Z is the result of X divided by Y rounded to 1dp.

x = float( input('Enter first number: ') ) y = float( input('Enter second number: ') ) print( f'{x} / {y} = {x/y:.1f}' )

Exercise 5.5

  1. Assign the numbers 4.5735, 9.4253 and 1.223 to the variables x, y, and z respectively.

  2. Using an f-string print each number rounded to 2dp and separated by a tab.

x = 4.5735 y = 9.4253 z = 1.223 print( f'{x:.2f}\t{y:.2f}\t{z:.2f}' )