0% found this document useful (0 votes)
8 views3 pages

Python input() Function Explained

This document provides a comprehensive guide on Python's input() function, detailing its behavior, return types, and best practices. It emphasizes that input() always returns a string, regardless of the user input, and discusses type conversion and common mistakes. Additionally, it includes tips for handling whitespace, validating input, and notes on historical differences with Python 2.

Uploaded by

Indian Aryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python input() Function Explained

This document provides a comprehensive guide on Python's input() function, detailing its behavior, return types, and best practices. It emphasizes that input() always returns a string, regardless of the user input, and discusses type conversion and common mistakes. Additionally, it includes tips for handling whitespace, validating input, and notes on historical differences with Python 2.

Uploaded by

Indian Aryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python input () Function — Complete

Guide
This document explains how Python's input() function works, key behaviors, return types,
conversions, common mistakes, and best practices. It is intended as a concise but complete
reference.

1. What input() Does


input(prompt) reads text typed by the user until they press Enter, then returns it as a string.
If a prompt is provided, it is displayed before waiting for input.

2. Most Important Rule


input() ALWAYS returns a value of type str (string), regardless of what the user types.

Examples:

 User types: 10 -> Stored as: "10" (str)


 User types: 5.6 -> Stored as: "5.6" (str)
 User types: True -> Stored as: "True" (str)

3. Return Type
Type returned: str

Code:

x = input()
print(type(x))

Output:

<class 'str'>

4. Basic Syntax
value = input("Enter something: ")

 Displays the prompt text (if provided)


 Waits until the user presses Enter
 Returns the entered text as a string
 Stores the string in the variable
5. Type Conversion
Convert to integer: x = int(input("Enter a number: "))

Convert to float: x = float(input("Enter a decimal: "))

6. String vs Numeric Addition


If you do not convert input to int or float, addition becomes string concatenation.

Example:

a = input()
b = input()
print(a + b)

Input:
10
20

Output:
1020

7. Reading Multiple Values


Read two integers on one line:

a, b = map(int, input().split())

Read a list of integers:

nums = list(map(int, input().split()))

8. Whitespace Handling
Use strip() to remove leading/trailing spaces:

name = input().strip()

9. Errors to Watch For


 ValueError occurs if conversion fails (e.g., int("abc"))
 Program pauses until user presses Enter
 input() only works in interactive contexts
10. Validation Example
try:
x = int(input())
except ValueError:
print("Invalid number")

11. Python 2 Note (Historical)


Python 2 used raw_input(). In Python 3, input() performs the same behavior as raw_input()
did.

12. Best Practices


 Always convert to int or float for numeric input
 Validate user input
 Use descriptive prompts
 Strip leading/trailing spaces when needed
 Handle empty input safely

13. Quick Reference Summary


 input() returns str
 Use int() / float() / bool() when needed
 Use split() and map() for multiple values
 Be prepared for ValueError
 input () pauses program execution

IMPORTANT:
If we have explicitly done Typecasting into int than if we enter string or any other data type
object (like String, float, char) than there will be error.

You might also like