# Type Conversion and Strings

> 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:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 11 min

**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

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

```python
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 = 3
```
Convert the text before doing maths, and convert back before joining it to more text.

![Six small panels. Type conversion turns the text 42 into the number 42. Concatenation joins Hello and Emma into one string. A tuple holds three fixed slots. An enum lists only RED, AMBER and GREEN. A built-in function len gives 5 for hello. Hard-coding hides the tax rate inside a sum.](https://learn.benibix.com/diagrams/dev7-working-with-values.svg)

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

**Words in this lesson:** [Type Conversion](https://learn.benibix.com/term/development/type-conversion), [String Concatenation](https://learn.benibix.com/term/development/string-concatenation), [Enumeration (Enum)](https://learn.benibix.com/term/development/enum)

**Question:** What does "Score: " + 10 do in Python?

**Answer:** It fails, because text and a number cannot be joined. Python will not join a string and a number. It stops with a TypeError. Use str(10) or an f-string to turn the number into text first.

---

Source: https://learn.benibix.com/learn/development-concepts/type-conversion-and-strings
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
