The input()
function in Python is used to take input from the user via the console. The input received is always in the form of a string, and you can process or convert it as needed.
Syntax:
input(prompt)
prompt
: A string message displayed to the user before input is entered. (Optional)
Console Input Function in Python
The input()
function in Python is used to take input from the user via the console. The input received is always in the form of a string, and you can process or convert it as needed.
Syntax:
input(prompt)
prompt
: A string message displayed to the user before input is entered. (Optional)
Basic Example:
name = input("Enter your name: ")
print(f"Hello, {name}!")
Output:
Enter your name: Ram
Hello, Ram!
Key Points:
- Returns a String: Regardless of the input type,
input()
always returns a string. - Conversion Required: If numeric input is needed, convert the string to an integer or float using
int()
orfloat()
.
Examples:
Accepting String Input
city = input("Which city do you live in? ")
print(f"You live in {city}.")
Output:
Which city do you live in? Bengaluru
You live in Bengaluru.
Accepting Integer Input
age = int(input("Enter your age: "))
print(f"Your age is {age}.")
Output:
Enter your age: 25
Your age is 25.
Accepting Float Input
salary = float(input("Enter your monthly salary: "))
print(f"Your salary is {salary:.2f}.")
Output:
Enter your monthly salary: 45000.50
Your salary is 45000.50
Multiple Inputs in One Line
- Use
split()
to take multiple inputs separated by spaces:
x, y = input("Enter two numbers separated by space: ").split()
print(f"First number: {x}, Second number: {y}")
Multiple Inputs in One Line
- Use
split()
to take multiple inputs separated by spaces:
x, y = input("Enter two numbers separated by space: ").split()
print(f"First number: {x}, Second number: {y}")
Output:
Enter two numbers separated by space: 10 20
First number: 10, Second number: 20
Code Explanation:
x, y = input("Enter two numbers separated by space: ").split()
input()
:- Displays the prompt
"Enter two numbers separated by space:"
and waits for user input. - The user enters two numbers separated by a space (e.g.,
10 20
).
- Displays the prompt
split()
:- Splits the input string into a list of substrings based on spaces by default.
- For example, if the input is
10 20
,split()
converts it into['10', '20']
.
- Unpacking:
- The two values in the list returned by
split()
are unpacked into the variablesx
andy
. x
gets the value'10'
(as a string), andy
gets the value'20'
(as a string).
- The two values in the list returned by
Using a Default Prompt
user_input = input()
print(f"You entered: {user_input}")
Output:
Hello, World!
You entered: Hello, World!
Code Explanation:
user_input = input()
input()
:- Displays no prompt message to the user.
- Waits for the user to type some input and press the Enter key.
- The input entered by the user is returned as a string.
user_input
:- Stores the string value entered by the user.
print(f"You entered: {user_input}")
- Formatted String:
- Prints a message displaying the value entered by the user.
- The variable
user_input
is interpolated into the string using an f-string.
Console Output function
The print function is used to display output in the console. The print function can take one or multiple arguments and converts them to strings and then displays them in the console.
Syntax:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters:
*objects
: The values or objects to be printed. Multiple items can be passed, separated by commas.sep
: Specifies the separator between multiple objects (default is a single space' '
).end
: Specifies what to append at the end of the output (default is a newline'\n'
).file
: Specifies the output stream (default issys.stdout
, which is the console).flush
: IfTrue
, forces the output to be written immediately.
Example
print("Python", "is", "fun!")
#Output: Python is fun!
print("Python", "is", "fun!", sep="-")
#Output: Python-is-fun!
print("Learning", end=" ")
print("Python")
#Output: Learning Python
Comments in Python Programming
Comments in Python are used to explain code, making it more readable and easier to understand. They are ignored by the Python interpreter and are not executed. There are two ways to write comments in Python:
# Single-line Comments
# This is a single-line comment
x = 10 # This is also a single-line comment
# Multi-line Comments
'''
This is a
multi-line comment
written using single quotes.
'''
"""
This is also a
multi-line comment
written using double quotes.
"""
To buy Python books click the below links
Python Programming
Python Programming 2E
Handbook of Python Programming