Type Conversion and Strings
Level 2 · Values and variables
How to turn text into a number and back. How to join strings, and how an enum gives a value a short list of allowed names.
Course contentsLesson 7 of 18
Level 1 · What a program is
Level 2 · Values and variables
Level 3 · Decisions and loops
Level 4 · Functions
Level 5 · Errors and debugging
Level 6 · Good code habits
After this lesson you can
- Convert text to a number with int and float
- Join strings and numbers safely
- Use an enum for a fixed list of choices
11 mincovers 3 glossary entries
Worth reading first: Data Types in Python
How to turn one type of value into another
Type conversion changes a value from one type into another. In Python, int("42") turns the text 42 into the number 42. Then str(42) turns it back into text. You need it all the time. Anything a user types arrives as a string, even when it looks like a number.
Python has a function for each main type: int, float, str and bool. Each one takes a value and gives back a new value of that type. float("3.5") gives 3.5. int(3.9) gives 3, because it cuts off the decimal part rather than rounding it.
Not every value can convert. int("hello") fails, and so does int("4.5"), because the text is not a whole number. Python stops with a ValueError. So when the text comes from a person, expect that it may be wrong, and check it first.
Joining strings end to end is called string concatenation. In Python the + sign does it, so "Hello, " + "Emma" gives "Hello, Emma". The same sign adds numbers. So Python must know the types before it can tell which job you mean.
That is why "Age: " + 30 fails with a TypeError. Python will not guess whether you meant text or maths. Convert first, with "Age: " + str(30). A shorter way is an f-string: f"Age: {age}". The f before the quote mark tells Python to drop the value of age into the text.
Some values should come from a short, fixed list, such as the three colours of a traffic light. An enum is a type built for that. In Python you import Enum from the enum module and list the names, such as RED, AMBER and GREEN. Any other value is then simply not allowed.
Without an enum, people use plain strings like "red" and "Red" and "RED" in different places. Those are three different strings to Python, and the bug can hide for weeks. An enum gives each choice exactly one spelling.
age_text = "30" # what a user typed
age = int(age_text) # now the number 30
print("Age: " + str(age + 1))
print(f"Next year: {age + 1}")
# Age: 31
# Next year: 31
from enum import Enum
class Light(Enum):
RED = 1
AMBER = 2
GREEN = 3Worked example
Jack's age form adds wrong
Jack's form asks for an age and adds 1. A user types 30, and the program prints 301 instead of 31.
The age was still text, so + joined "30" and "1" as strings. He wraps the input in int() first. Now 30 + 1 gives 31.
The words for this lesson3
Check yourself
What does "Score: " + 10 do in Python?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.