Python Programming

Input

Easy
20 min

What is input?

Programs typically receive input from outside the program. Otherwise, the program would always follow exactly the same path.

Input can come from, for example:

  • From the terminal (asks the user for something).
  • As a command line parameter (run the program like python3 app.py dippadappaduu, where dippadappaduu would be the first command line parameter).
  • From file on disk
  • From the online service
  • From an HTTP request, if it is a web application
  • ...and from anywhere else.

input function

We have so far become acquainted with only one of Python's built-in functions, print, which prints to the terminal. Now we will get to know its counterpart, input, which reads input from the terminal.

name = input("What is your name?")
print(name)
python3 ./app.py
What is your name? Act
Act

Merging Texts

In Python, texts can be combined with each other using the plus (+) operator.

print("A" + "B" + "C")
ABC

We can create a program that greets the user:

name = input("What is your name?")
print("Hello, " + name)
python3 ./app.py
What is your name? Act
Hello, Teo

Exercise

Example of how the program should work:

python3 ./app.py
What is the player's name? Act
Player 1: Action

Exercises

Task 1

Make a program that asks for a player's name and then prints: "Pelaaja 1: Annettu nimi"

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.