AlgoBazaAlgoBaza
Python for kids ยท Module 1

Lesson 14

input() - reading text

After this lesson: Your program will read text from the input and use it in its answer.

Video

?

The video for this lesson is coming soon.

Explanation

So far programs always printed the same thing. Now we'll teach them to listen - the program will read data and answer it!

Input and output

Every program in a task has two sides:

  • input - the data the program gets (e.g. a name, numbers),
  • output - what the program prints with print.

The same program can get different data and answer differently each time - like a calculator that calculates whatever you type. The judge runs your program many times, each time with different data.

input() - the program asks, you answer

input() reads one line from the input. It's best to save it straight away in a variable:

text = input()

What happens in this one line - step by step:

  1. The program reaches input() and waits.
  2. You (or the judge) type a line of text and press Enter โŽ.
  3. input() gives back that line as text, and the = sign saves it in the variable text.
  4. The program carries on - and can use the variable text, e.g. in print.

In the examples below you'll try it yourself: the program stops at input(), and you type the text on the screen. โŒจ๏ธ

Remember Enter! โŽ

When the program waits at input(), type your text on the screen and press the Enter key โŽ (or click the yellow โŽ Enter button next to it). Only then does the program get your text and go on. Without Enter the program keeps waiting.

Bajtek here! ๐Ÿค– In code these 4 steps are one line: text = input(). We read it from the right - first input() waits and gives back the text (e.g. "Ala"), then the = sign saves it in the variable text. From now on text remembers "Ala", and print can show it on the screen!

Your first input() โŒจ๏ธ

1) Click the arrow. 2) At input() the program stops - type something on the screen. 3) Press Enter โŽ. 4) The last line prints your text.

๐Ÿ“ฅinput()
โžก๏ธwaits for
โŒจ๏ธkeyboard
โžก๏ธthe typed text
๐Ÿ“ฆ text?

Click the arrow. First input() starts, then it waits until you type something on the keyboard.

Code
1print("Type your first text:")
2text = input()
3print("You wrote:", text)
Screen
โ–
1/3 ยท line 1 runs next

The parrot repeats ๐Ÿฆœ

Type anything and press Enter โŽ - the parrot repeats it twice.

๐Ÿ“ฅinput()
โžก๏ธwaits for
โŒจ๏ธkeyboard
โžก๏ธthe typed text
๐Ÿ“ฆ word?

Click the arrow. First input() starts, then it waits until you type something on the keyboard.

Code
1word = input()
2print(word)
3print(word)
Screen
โ–
1/2 ยท line 1 runs next

A greeting

Type your name ๐Ÿง’ and press Enter โŽ.

๐Ÿ“ฅinput()
โžก๏ธwaits for
โŒจ๏ธkeyboard
โžก๏ธthe typed text
๐Ÿ“ฆ name?

Click the arrow. First input() starts, then it waits until you type something on the keyboard.

Code
1name = input()
2print("Hi,", name + "!")
3print("Nice to meet you,", name)
Screen
โ–
1/2 ยท line 1 runs next

A Christmas card ๐ŸŽ„

Type a name ๐Ÿง’ and press Enter โŽ - the program puts it into the wishes.

๐Ÿ“ฆ name: ?
๐ŸŽ„๐ŸŽ„
โ€ฆ

Type a name on the screen and press Enter - we'll make a card with it.

Code
1name = input()
2print("*" * 20)
3print("Merry Christmas,", name + "!")
4print("*" * 20)
Screen
โ–
1/2 ยท line 1 runs next

Several input() calls in a row

Every next input() reads the next line - like taking cards off the top of a pile. The first input() takes the first line, the second one the second line, and so on. That's why you press Enter after each one.

The input belt

The program waits for two lines: first a name ๐Ÿง’, then an animal ๐Ÿถ. Press Enter โŽ after each one. Every input() takes one line.

input() no. 1๐Ÿ“ฆ name?
input() no. 2๐Ÿ“ฆ pet?

Click the arrow. Every input() takes one line - in turn, from the top.

Code
1name = input()
2pet = input()
3print("Hello,", name)
4print(name, "has a pet:", pet)
Screen
โ–
1/2 ยท line 1 runs next

The same program, different data

The same program again - this time type a completely different name ๐Ÿง’ and animal ๐Ÿถ. Press Enter โŽ after each one. The code is the same, but the result is different! That's how the judge runs your program with different data.

input() no. 1๐Ÿ“ฆ name?
input() no. 2๐Ÿ“ฆ pet?

Click the arrow. Every input() takes one line - in turn, from the top.

Code
1name = input()
2pet = input()
3print("Hello,", name)
4print(name, "has a pet:", pet)
Screen
โ–
1/2 ยท line 1 runs next

Favourite things

The program asks for three things in turn: a favourite animal ๐Ÿถ, colour ๐ŸŽจ and food ๐Ÿ•. Press Enter โŽ after each one.

input() no. 1๐Ÿ“ฆ animal?
input() no. 2๐Ÿ“ฆ colour?
input() no. 3๐Ÿ“ฆ food?

Click the arrow. Every input() takes one line - in turn, from the top.

Code
1animal = input()
2colour = input()
3food = input()
4print("I like", animal)
5print("My colour is", colour)
6print("For dinner:", food)
Screen
โ–
1/2 ยท line 1 runs next

A name badge ๐Ÿท๏ธ

Type a name ๐Ÿง’, then a class ๐Ÿซ (e.g. 5b). Press Enter โŽ after each one.

๐Ÿ“ฆ name: ?๐Ÿ“ฆ form: ?
๐Ÿท๏ธ
โ€ฆ

Type a name, then a class - each on its own line. Then we'll make a badge.

Code
1name = input()
2form = input()
3print("+--------------+")
4print("| " + name)
5print("| class " + form)
6print("+--------------+")
Screen
โ–
1/2 ยท line 1 runs next

A story from three words ๐Ÿ“–

Type three lines: a hero ๐Ÿ‰ (e.g. dragon), a place ๐Ÿฐ (e.g. castle) and an object ๐Ÿ—๏ธ (e.g. golden key). Press Enter โŽ after each one.

๐Ÿ“ฆ hero: ?๐Ÿ“ฆ place: ?๐Ÿ“ฆ thing: ?
๐Ÿ“–
โ€ฆ

Type a hero, a place and a thing - each on its own line. A story will be made from these three words.

Code
1hero = input()
2place = input()
3thing = input()
4print("Once upon a time", hero, "lived in", place + ".")
5print("One day he found", thing + "!")
Screen
โ–
1/2 ยท line 1 runs next

Joining and repeating text

You can do two tricks with text:

  • + joins texts without a space: "Algo" + "Baza" gives AlgoBaza, and name + "!" adds an exclamation mark.
  • * repeats text: "ha" * 3 gives hahaha.

Compare with a comma in print: a comma adds a space, a plus doesn't.

Plus and times on text

Type a short word (e.g. ha) and press Enter โŽ - see what + and * do with it.

๐Ÿ“ฆ word: ?
?

Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.

Code
1word = input()
2print(word + "!")
3print(word * 3)
4print(word, "!")
Screen
โ–
1/2 ยท line 1 runs next

Joining two words

Type two words, each on its own line (e.g. Algo and Baza). Press Enter โŽ after each one.

๐Ÿ“ฆ first_word: ?๐Ÿ“ฆ second_word: ?
?

Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.

Code
1first_word = input()
2second_word = input()
3print(first_word + second_word)
4print(first_word, second_word)
5print(second_word + first_word)
Screen
โ–
1/2 ยท line 1 runs next
?

The input is the line Bajtek. What does the program print?

name = input()
print("name")
?

The input has the lines Ala and Ola. What does the program print?

first_word = input()
second_word = input()
print(second_word + first_word)
?

What does print("ab" * 2 + "!") print?

Text from input can be repeated

Type a word and press Enter โŽ - the text you read is ordinary text, so + and * work on it.

Code
1word = input()
2print(word * 3)
3print(word + "!" + word)
4print("-" * 10)
Screen
โ–
1/2 ยท line 1 runs next

Joining with input

If the word "ole" was read, the expression word + "!" + word works out like this:

"ole" + "!" + "ole"
1/3 ยท click the arrow - Python works out the next part
?

The input is two lines: Cat and Dog. What does the program print?

a = input()
b = input()
print(b, a)
?

The input is one line: 7. What does print(input() * 2) print?

Bajtek shouts in the mountains ๐ŸŽฌ

Click Next and see how + and * make an echo out of a word.

Bajtek
โ›ฐ๏ธ"hop"โžก๏ธ๐Ÿ“ฆword

Code

word = input()

๐Ÿ–ฅ๏ธ Screen

hop
1

At the top

Bajtek climbs to the top of a mountain โ›ฐ๏ธ and wants to shout. The program asks: word = input(). Bajtek types hop and presses Enter - the text "hop" goes into the box word.

Step 1 / 5

An echo in the mountains โ›ฐ๏ธ

Shout something in the mountains: type a word (e.g. hop) and press Enter โŽ.

๐Ÿ“ฆ word: ?
?

Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.

Code
1word = input()
2print(word + "!")
3print(word * 2 + "!")
4print(word * 3 + "!")
Screen
โ–
1/2 ยท line 1 runs next

Remember

  • input() reads one line from the input - always as text.
  • Every next input() reads the next line.
  • In tasks write just input() - no text in the brackets.
  • + joins texts without a space, * repeats them.

In the tasks the examples show what the input will be. Your program has to work for any data - not just the example!

Tasks from the video

We solve them together in the video - pause it and try first on your own.

Practice tasks

Try to solve them without the video. Stuck? Every task has hints and an editorial.

Sign in to save your progress.