Converting numbers to text and back

Now you know how to store numbers and how to store text. Being able to convert the two into each other will come in handy. For that, we will use type conversions.

Warming up

Now we are going to combine strings with integer numbers.

name = 'Emily Smith'
born = _____
____ = '15'

text = ____ + ' was born in the year ' + _____
year = born + _____
text
year

Insert into the following items into the code, so that all statements are working: age, int(age), name, str(born), 2000

Questions

  • Can you leave str(born) and int(age) away?
  • What do str() and int() do?

Exercises

Exercise 1:

What is the result of the following statements?

 9 + 9

 9 + '9'

 '9' + '9'

Exercise 2:

Change the statements above by adding int() or str() to each of them, so that the result is 18 or '99', respectively.

Exercise 3:

Explain the result of the following operations?

 9 * 9
 9 * '9'
 '9' * 9

Exercise 4:

Write Python statements that create the following string:

12345678901234567890123456789012345678901234567890

The Challenge: A data record with types

field value type
first name Andrew string
last name O'Malley string
gender M string
year of birth 2000 integer
age 15 integer

Write the values from each row of the table into string or integer variables, then combine them to a single one-line string.

results matching ""

    No results matching ""