Text and numbers together
After this: Convert between text and numbers, and explain the 34 surprise.
On this page · 5 sections · 3 checks
Someone types their age into a form on a website: a 3, then a 0. What reaches the program is not the number 30. It is the text "30": two characters that happen to be digits. Text that looks like a number is still text. This lesson is about the trouble that causes, and the fix.
The surprise
Lesson 2 said that + adds numbers and joins text. Put the two facts together and watch what happens.
- Run the programPress Run it and step forward. The first line joins two pieces of text; the second turns the text into a number first.
print("3" + "4")print(int("3") + 4)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
"3" + "4": both sides are in quotes, so both are text, and+joins text. The result is the text34. Not 7. The computer did what the kinds told it to do.- int("3") + 4Integer.parseInt("3") + 4: the first part turns the text
"3"into the number 3. Now both sides are numbers, so+adds. The result is7.
Turning text into a number
int(...)Integer.parseInt(...) is an instruction that gives back the whole number written in a piece of text. Give it "3" and you get the integer 3. For text with a decimal point, use float(...)Double.parseDouble(...), which gives a float.
print(float("3.5") + 1)4.5
System.out.println(Double.parseDouble("3.5") + 1);4.5
If the text is not a number at all, such as "abc", the instruction cannot do its job and the program stops with an error. A later tutorial in this learning path, the one about errors, shows how to deal with that.
Turning a number into text
Now the other direction. You have the number 30 in a box named age, and you want to print Age: 30.
age = 30print("Age: " + str(age))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Run it
Put different digits inside the quotes, and then try turning both pieces of text into numbers on the first line as well.
Check yourself
What does this print?
print("3" + "4")print(int("3") + 4)System.out.println("3" + "4");System.out.println(Integer.parseInt("3") + 4);Complete the second line so that the program prints 7: turn the text in the box text into a whole number, and put it in a box named number.
text = "3"____ # ← put the whole number written in text into a box named numberprint(number + 4)String text = "3";____ // ← put the whole number written in text into a box named numberSystem.out.println(number + 4);What does this print?
a = "10"b = 5print(int(a) + b)print(a + str(b))String a = "10";int b = 5;System.out.println(Integer.parseInt(a) + b);System.out.println(a + b);- Explain the surprise: "3" + "4" is the text 34, because + joins text.
- Turn text into a number with int(...) in Python or Integer.parseInt(...) in Java.
- Turn a number into text with str(...) in Python; Java does it for you when one side of + is text.