Basics · lesson 5 of 10
Parts in this lesson
Alen()how many characters
B[0] [-1] [4:10]by position, and slices
C.strip() .upper()methods: things text can do
Dinput()reads what's typed
Eint()from 1.4: text to a number

Text and input

Last updated

So far your programs have printed the same thing every time. In this lesson you'll take text apart by position, tidy it with methods, read what the person using your program types, and turn typed digits into a number, including the error you get when they aren't digits.

By the end of this lesson you can

  • Measure text with len(), and read characters and slices by position
  • Use string methods like .strip(), .upper() and .title()
  • Read a line the user types with input()
  • Turn typed text into a number with int(), and read the ValueError when it can't be

How long is it?

len() gives the number of characters in some text, spaces included. "The Hobbit" has 10: 9 letters and a space. Line 2 of the program below prints it. Ignore the other lines for now; they're for the next step.

Step 1 code main.py
Edit the code, then run it again

Reading by position

Each character has a position, called its index, and the first one is at position 0, not 1. title[0] is the first character, T. title[4] is the fifth, H.

A minus sign counts from the end: title[-1] is the last character. And title[4:10] is a slice: the characters from position 4 up to, but not including, position 10. Leave out the first number and the slice starts at the beginning (title[:3]).

Watch it run, and line each answer up with the picture below.

Step 2 code main.py
Edit the code, then run it again
 T  h  e     H  o  b  b  i  t
 0  1  2  3  4  5  6  7  8  9

Counting from 0 feels odd at first, but nearly every programming language does it. A good way to think of it: the index is how many characters you skip from the start.

Methods: things text can do

Text comes with built-in actions called methods. You use one by writing a dot after the text, then the method's name and brackets: typed.strip(). .strip() removes spaces from both ends, .upper() makes every letter a capital, and .title() capitalises each word.

The square brackets in the output are there so you can see where the spaces are.

Step 3 code main.py
Edit the code, then run it again

Look at the last line: typed hasn't changed. Text in Python can never be changed. A method gives back new text, and the original stays as it was. To keep the result, give it a name: clean = typed.strip(). Line 4 also shows that methods can be chained: .strip() gives new text, and .title() runs on that.

Asking the user: input()

input() waits for the person using the program to type a line, then gives back what they typed as text. The text in its brackets is a prompt, printed first so they know what to type.

In this page, what the user types comes from the Input box under the code. Each input() reads the next line from it, and the output shows the line as if it had been typed, just like in a terminal. Change the name in the box and run it again.

Step 4 code main.py
Edit the code, then run it again

Turning text into a number

input() always gives back text, even when the person types digits. To do arithmetic with what they typed, turn it into a number with int() (from lesson 1.4) for whole numbers, or float() for decimals.

Line 1 does both jobs in one go: input() reads the text, and int() turns it into a number. Try a few values in the box.

Step 5 code main.py
Edit the code, then run it again

Remove int( and the closing ) on line 1, then run it with 4 again. There's no error, but the fine is 4 written 25 times. Multiplying text by a number repeats it. This mistake is common, and it's why reading input almost always comes with int() or float().

When the text isn't a number

int() can only turn digits into a number. The input box below has ten in it. Press Run and read the error from the bottom up.

Step 6 code main.py
Edit the code, then run it again

ValueError: invalid literal for int() with base 10: 'ten'. Literal means the text as written, and base 10 means ordinary digits. The text had the right type, str, but a value int() can't use, so it's a ValueError, not a TypeError. Change ten to 10 and run it again.

Real people type the wrong thing all the time. In lesson 2.6 you'll learn to catch this error and ask again instead of stopping.

Check what you learned

Four questions. Pick or type an answer, then check it.

word = "Emma". What is word[1]?
After name = " ada " and name.strip(), what is name?
What kind of value does input() always give back?
Which line stops with a ValueError?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python