← Lessons/Variables & Output
← Variables & Output

Convert a string to a number

Learn

The number 42 vs. the text "42"

Every value has a type. 42 is an integer, a number you can do arithmetic on. "42" is a string, text that happens to be made of digits. They behave completely differently:

42 * 2       # 84     — arithmetic
"42" * 2     # "4242" — string repetition!

This matters because data from the outside world always arrives as text: from files, environment variables, command-line arguments, API responses. Convert it first with int(...), float(...) or str(...):

port = "8080"              # a string (e.g. read from a config file)
next_port = int(port) + 1  # convert, THEN do arithmetic → 8081

The editor opens with your script from the previous step: add to it.

The variable count is the string "42". Convert it to an integer, double it, and print doubled: 84.

Interview prep

Knowing that input arrives as strings (and must be converted) heads off a whole class of bugs.

What's the difference between "42" and 42 in Python, and why does it matter?

"42" is a string (text); 42 is an integer. They behave differently — "42" * 2 is "4242" (repetition), while 42 * 2 is 84. Data read from files, environment variables, or CLI arguments always arrives as strings, so you convert with int()/float() before doing arithmetic.

$ python3 main.py
Loading...

Output

Run your solution to see its output here.