In Python and many other programming languages you can get user input. Do not worry, you do not need to write a keyboard driver.

The input() function will ask keyboard input from the user. If you are still using Python 2, you have the function raw_input().

Related course: Complete Python Programming Course & Exercises

Example

The input function prompts text if a parameter is given. The functions reads input from the keyboard, converts it to a string and removes the newline (Enter).

Type and experiment with the script below (save as key.py)

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python3

name = input('What is your name? ')
print('Hello ' + name)

job = input('What is your job? ')
print('Your job is ' + job)

num = input('Give me a number? ')
print('You said: ' + str(num))

Output should be something like this, depending on your terminal:

keyboard input

By the time you are reading this, perhaps you are used to voice input or other types of human-computer interaction. Eitherway keyboard input is still very useful for coding.

If you are a beginner, then I highly recommend this book.

Exercise

Try these exercises:

  1. Make a program that asks a phone number.
  2. Make a program that asks the users preferred programming language.

After completing these continue with the next exercise.

Download examples