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 theValueErrorwhen 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.
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.
T h e H o b b i t
0 1 2 3 4 5 6 7 8 9Counting 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.
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.
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.
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.
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.
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.
- The Python Tutorial: An Informal Introduction to Python, Text: indexing and slicing text
- Built-in Types: Text Sequence Type (str)
- Built-in Types: String Methods: strip, upper, lower, title, replace
- Built-in Functions: len()
- Built-in Functions: input()
- Built-in Functions: int()
- Built-in Exceptions: ValueError