• Home
  • 2.2 Reading Input from the User

2.2 Reading Input from the User

View Categories

2.2 Reading Input from the User

Most programs are interactive, meaning they allow users to provide data while the program is running. Instead of using fixed values, a program can ask the user to enter information such as their name, age, salary, or marks.

Python provides the built-in input() function to read data from the keyboard.

The input() function always waits for the user to type something and press the Enter key.


Syntax #

input(prompt)

Parameter #

Parameter Description
prompt An optional message displayed before waiting for user input.

Return Value #

The input() function always returns the entered value as a string (str), regardless of what the user types.


Example 1: Reading Input Without a Prompt #

main.py

name = input()

print(name)

Sample Input #

Alice

Output #

Alice

Explanation #

  • input() waits for the user to enter data.
  • The user types Alice and presses the Enter key.
  • The entered text is stored in the variable name.
  • print(name) displays the value stored in the variable.

Example 2: Reading Input With a Prompt #

main.py

name = input("Enter your name: ")

print("Welcome", name)

Sample Input #

Enter your name: Alice

Output #

Welcome Alice

Explanation #

  • The string "Enter your name: " is displayed before waiting for input.
  • The user enters Alice.
  • The entered value is stored in the variable name.
  • The print() function displays a welcome message along with the entered name.

Example 3: Reading an Integer #

main.py

age = int(input("Enter your age: "))

print(age)

Sample Input #

Enter your age: 25

Output #

25

Explanation #

  • input() always returns a string.
  • The int() function converts the string into an integer.
  • The converted value is stored in the variable age.
  • Since age is now an integer, it can be used for mathematical calculations.

Example 4: Reading a Floating-Point Number #

main.py

salary = float(input("Enter your salary: "))

print(salary)

Sample Input #

Enter your salary: 35000.75

Output #

35000.75

Explanation #

  • float() converts the entered string into a floating-point number.
  • Floating-point numbers are used to represent values containing a decimal point.
  • The converted value is stored in the variable salary.

Example 5: Reading Multiple Values #

main.py

first_name, last_name = input("Enter your full name: ").split()

print(first_name)
print(last_name)

Sample Input #

Enter your full name: John Smith

Output #

John
Smith

Explanation #

  • input() reads the entire line entered by the user.
  • .split() divides the string into separate parts using spaces.
  • The first word is assigned to first_name.
  • The second word is assigned to last_name.

Example 6: Reading Multiple Integers #

main.py

a, b = map(int, input("Enter two numbers: ").split())

print(a)
print(b)

Sample Input #

Enter two numbers: 10 20

Output #

10
20

Explanation #

  • input() reads the entire line.
  • .split() separates the values based on spaces.
  • map(int, ...) converts every value into an integer.
  • The converted integers are assigned to a and b.

Example 7: Reading a List of Integers #

main.py

numbers = list(map(int, input("Enter numbers: ").split()))

print(numbers)

Sample Input #

Enter numbers: 10 20 30 40 50

Output #

[10, 20, 30, 40, 50]

Explanation #

  • The user enters multiple numbers separated by spaces.
  • .split() converts the input into a list of strings.
  • map(int, ...) converts each string into an integer.
  • list() converts the result into a Python list.

Example 8: Reading a Sentence #

main.py

sentence = input("Enter a sentence: ")

print(sentence)

Sample Input #

Enter a sentence: Python is easy to learn.

Output #

Python is easy to learn.

Explanation #

  • input() reads the complete line entered by the user.
  • Spaces are preserved.
  • The entire sentence is stored as a single string.

Example 9: Reading a Single Character #

main.py

ch = input("Enter a character: ")[0]

print(ch)

Sample Input #

Enter a character: Apple

Output #

A

Explanation #

  • input() reads the complete string entered by the user.
  • [0] accesses the first character of the string.
  • Even if multiple characters are entered, only the first one is stored.

Example 10: Verifying the Return Type of input() #

main.py

value = input("Enter any value: ")

print(type(value))

Sample Input #

Enter any value: 100

Output #

<class 'str'>

Explanation #

  • Even though the user entered a number, input() returns a string.
  • The type() function displays the data type of a variable.
  • This is why type conversion using int() or float() is often required.

Common Mistakes #

1. Assuming input() Returns an Integer #

Incorrect

age = input("Enter your age: ")

print(age + 5)

Sample Input #

25

Output #

TypeError: can only concatenate str (not "int") to str

Reason

input() returns a string, so Python cannot add an integer to it.

Correct

age = int(input("Enter your age: "))

print(age + 5)

2. Forgetting to Use .split() #

Incorrect

a, b = input()

Sample Input #

10 20

Output #

ValueError: too many values to unpack

Reason

Without .split(), the entire input is treated as one string instead of two separate values.

Correct

a, b = input().split()

3. Entering Non-Numeric Data for int() #

Incorrect Input

age = int(input("Enter age: "))

Sample Input #

abc

Output #

ValueError: invalid literal for int() with base 10: 'abc'

Reason

The entered text cannot be converted into an integer.


4. Accessing the First Character of an Empty String #

Incorrect

ch = input()[0]

Sample Input #

(User presses Enter without typing anything.)

Output #

IndexError: string index out of range

Reason

The input string is empty, so there is no first character.


Key Points to Remember #

  • input() is the built-in function used to read keyboard input.
  • The prompt parameter is optional.
  • input() always returns a string.
  • Use int() to convert input into an integer.
  • Use float() to convert input into a floating-point number.
  • Use .split() to read multiple values from one line.
  • Use map() to convert multiple input values into another data type.
  • A single character can be obtained using string indexing.
  • Always validate user input in real-world applications.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *