Understanding the Python input() Function
Last Updated: 02 Jul, 2024
The input()
function in Python is used to take user input. By default, it returns the input in the form of a string. This functionality makes it a vital part of any interactive Python program.
Syntax of the input() Function
input(prompt)
Parameters:
prompt
(optional): A string value to display as an input message.
Example:
input("What is your favorite fruit? ")
How the input() Function Works
By default, the input()
function helps in taking user input as a string. If you wish to convert it to an integer or float, you can simply typecast it.
Examples
Example 1: Taking Input as String
favorite_fruit = input("What is your favorite fruit?: ")
print(favorite_fruit)
Output:
What is your favorite fruit?: Mango
Mango
Example 2: Taking Input as Integer
number_of_apples = int(input("How many apples do you have?: "))
print(number_of_apples)
Output:
How many apples do you have?: 7
7
Example 3: Taking Input as Float
price_per_apple = float(input("What is the price of each apple?: "))
print(price_per_apple)
Output:
What is the price of each apple?: 0.75
0.75
Combining Inputs
Example 4: Taking Name and City
user_name = input("Please Enter Your Name: ")
user_city = input("Please Enter Your City: ")
print("Name & City:", user_name, user_city)
Output:
Please Enter Your Name: Alice
Please Enter Your City: Wonderland
Name & City: Alice Wonderland
Example 5: Adding Two Integers
first_number = int(input("Please Enter First Number: "))
second_number = int(input("Please Enter Second Number: "))
sum_result = first_number + second_number
print("The sum is:", sum_result)
Output:
Please Enter First Number: 4
Please Enter Second Number: 6
The sum is: 10
Example 6: Taking Two Lists as Input
list_a = list(input("Please Enter Elements of List A: "))
list_b = list(input("Please Enter Elements of List B: "))
for element in list_b:
list_a.append(element)
print("Combined List:", list_a)
Output:
Please Enter Elements of List A: ab
Please Enter Elements of List B: cd
Combined List: ['a', 'b', 'c', 'd']
FAQs on the input() Function
1. How to use the input() function in a Python script?
The input()
function in Python reads a line from the input, converts it to a string, and returns it.
2. Can we provide a basic example of using the input() function?
name = input("Enter your name: ")
print("Hello, " + name + "!")
3. How to store the value entered by the user using the input() function?
You can store the value entered by the user in a variable.
user_input = input("Please enter something: ")
print("You entered:", user_input)
4. How does the input() function handle different data types?
The input()
function always returns the input as a string. If you need a different type, you must convert it manually.
5. How can we convert the input to an integer?
user_input = input("Enter a number: ")
number = int(user_input)
print("The number you entered is:", number)
Conclusion
The input()
function is a powerful tool in Python for obtaining user input. Understanding how to manage and convert this input is essential for developing interactive applications. With the examples and explanations provided, you should be well-equipped to handle user interactions effectively in your Python programs.
© 2025 Random Blog. All rights reserved.
0 Comments