Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 08

Reading Keyboard Input

Read data from the keyboard with input() and input(Message). Both return str.

Beginner15 min

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:

python
a = 10
b = 20

we allow the user to enter them from the keyboard.

User types data, input() stores it as str

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)

python
varname = input()

This function reads any type of keyboard data in the form of str.

python
# DataRead1.py
print("Enter Value:")
a = input()
print(a, type(a))
text
Enter Value:
10
10 <class 'str'>
Enter Value prompt, then 10 stored as "10"

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.

python
varname = input("Message")

Here, Message is the user prompting message.

python
# DataRead2.py
a = input("Enter Value:")
print(a, type(a))
text
Enter Value:10
10 <class 'str'>

Difference from input()

input() — prompt appears first, then input (DataRead1.py):

python
print("Enter Value:")
a = input()

input(Message) — prompt and cursor appear on the same line (DataRead2.py):

python
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
python
# DataReadStrEx.py
a = input("Enter Number:")
print(a)
print(type(a))

If the user enters 25:

text
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.

Keyboard sends 1 0; input() stores "10" as str

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.

python
# InputLoginEx.py
username = input("Enter Username:")
password = input("Enter Password:")
print("Username:", username)
print("Password:", password)
text
Enter Username:Mahesh
Enter Password:python123
Username: Mahesh
Password: python123

Usernames and passwords are naturally stored as strings.

Important note

We can convert the returned str into any required data type using type casting functions such as int() and float(). 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() and input(Message)
  • input(Message) simply displays a user prompting message
  • Both functions always return str
  • Values like 10, 3.14, and Python are initially stored as strings
  • Converting them into numbers is a separate step (DataRead3.py next)

Quick knowledge check

  1. How many keyboard input functions are introduced in this lesson?
    2

  2. Which function shows a prompt on the same line?
    input(Message)

  3. What data type does input() always return?
    str

  4. 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.