Lesson 08
Reading Keyboard Input
Read data from the keyboard with input() and input(Message). Both return str.
What you will learn
By the end of this lesson, you'll understand:
- What are the two pre-defined functions for dynamic keyboard input?
- How does
input()work without a message? - How does
input(Message)work with a prompting message? - Why is the return value always
str?
What is keyboard input?
A program becomes dynamic when it accepts values from the user while it is running.
Instead of hardcoding values:
a = 10
b = 20we allow the user to enter them from the keyboard.
According to your notes, both input functions always return string (str) data.
Two pre-defined functions
Python provides 2 pre-defined functions for reading keyboard input dynamically.
| Function | Purpose |
|---|---|
input() |
Read keyboard input |
input(Message) |
Show a prompt and read keyboard input |
The difference is only the prompt message—both return str.
1. input() (DataRead1.py)
varname = input()This function reads any type of keyboard data in the form of str.
# DataRead1.py
print("Enter Value:")
a = input()
print(a, type(a))Enter Value:
10
10 <class 'str'>Although you typed 10, Python stores it as "10", not the integer 10.
2. input(Message) (DataRead2.py)
This version displays the prompt and reads the input on the same line.
varname = input("Message")Here, Message is the user prompting message.
# DataRead2.py
a = input("Enter Value:")
print(a, type(a))Enter Value:10
10 <class 'str'>Difference from input()
input() — prompt appears first, then input (DataRead1.py):
print("Enter Value:")
a = input()input(Message) — prompt and cursor appear on the same line (DataRead2.py):
a = input("Enter Value:")Both methods produce the same stored result: a str.
Input always returns str
This is the most important rule from your notes.
| You type | Stored value | Data type |
|---|---|---|
10 |
"10" |
str |
3.14 |
"3.14" |
str |
Python |
"Python" |
str |
# DataReadStrEx.py
a = input("Enter Number:")
print(a)
print(type(a))If the user enters 25:
25
<class 'str'>Even numbers are stored as strings.
Why is everything a string?
Think of the keyboard as a device that sends text characters.
Python cannot assume whether 10 should be:
- an integer
- a float
- or simply text
So it safely stores everything as str.
Real-world example (InputLoginEx.py)
Imagine a login screen.
# InputLoginEx.py
username = input("Enter Username:")
password = input("Enter Password:")
print("Username:", username)
print("Password:", password)Enter Username:Mahesh
Enter Password:python123
Username: Mahesh
Password: python123Usernames and passwords are naturally stored as strings.
Important note
We can convert the returned
strinto any required data type using type casting functions such asint()andfloat(). That conversion is the next lesson.
For now, remember:
input()→ reads data- Result → always
str - Conversion → later using
int()/float()
Key terms
| Term | Meaning |
|---|---|
| Keyboard input | Data entered by the user |
| Dynamic input | Input provided while the program is running |
input() |
Reads keyboard data |
| Prompt message | Text shown to guide the user |
str |
String data type returned by input() |
Summary
- Python provides two pre-defined input functions:
input()andinput(Message) input(Message)simply displays a user prompting message- Both functions always return
str - Values like
10,3.14, andPythonare initially stored as strings - Converting them into numbers is a separate step (
DataRead3.pynext)
Quick knowledge check
-
How many keyboard input functions are introduced in this lesson?
2 -
Which function shows a prompt on the same line?
input(Message) -
What data type does
input()always return?
str -
If the user types
3.14, what is actually stored?
"3.14"(a string)
Next lesson: type casting with int() and float() to perform arithmetic on user input.