Posted in

Python input(): What is the input() function in Python?

Python input() function
Python input() function

Python input()

Syntax: input(‘Prompt)
Prompt (optional): Only one argument is required and it is also optional. We write a string in prompt, which we want to show the user while taking the input. Most important thing, whatever the user writes, the input() function always returns a string.

How to use the input() function in Python?

1: Without Prompt
2: With Prompt

Python input() function without prompt

Code:
print("Enter Your Name: ")
name = input()
print('Hello!', name)

Output:
Enter Your Name: 
Code:
print("Enter Your Name: ")
name = input()
print('Hello!', name)

Output:
Enter Your Name: 
Python
Hello! Python
Code:
print("Enter Your Name: ")
name = input()
print('Hello!', name)
print('Where are you from?')
place = input()
print('Hello', name, 'from', place)

Output:
Enter Your Name: 
Python
Hello! Python
Where are you from?
all around the world
Hello Python from all around the world
Code:
print("Enter Your Name: ")
name = input()
print('Hello ' + name)

Output:
Enter Your Name: 
Python
Hello Python
Code:
print("Enter Your Name: ")
name = input()
print(f'Hello {name}. Good day.')

Output:
Enter Your Name: 
Python
Hello Python. Good day.
Code:
print("Enter Your Name: ")
name = input()
print('Hello ' + name)
print(type(name))

Output:
Enter Your Name: 
Python
Hello Python
<class 'str'>
Code:
print("Enter Your Name: ")
name = input()
print('Hello ' + name)
print(type(name))

Output:
Enter Your Name: 
104
Hello 104
<class 'str'>
Code:
print("Enter Your birth Year: ")
number = input()
current_year = 2025 - number
print(f"You are {current_year} old.")

Output:
Enter Your birth Year: 
1991
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Code:
print("Enter Your birth Year: ")
number = input()
current_year = 2025 - int(number)
print(f"You are {current_year} old.")

Output:
Enter Your birth Year: 
1991
You are 34 old.
Code:
print("Enter Your birth Year: ")
number = input()
current_year = 2025 - int(number)
print(f"You are {current_year} old.")

Output:
Enter Your birth Year: 
Python
ValueError: invalid literal for int() with base 10: 'Python'

Python input() function with prompt

Code:
name = input('Enter your name: ')
print('Hello!', name)

Output:
Enter your name: Python
Hello! Python
Code:
name = input('Enter your name: \n')
print('Hello!', name)

Output:
Enter your name: 
Python
Hello! Python
Code:
name = input('What is your name?: ')
age = input('How old are you?: ')
fav_color = input('What is your favorite color?: ')
print(f'Hello {name}. You are {age} years old and your favorite color is {fav_color}.')

Output:
What is your name?: V
How old are you?: 33
What is your favorite color?: Red
Hello V. You are 33 years old and your favorite color is Red.

Conclusion

Click for the Python operators (Part 1) tutorial

One thought on “Python input(): What is the input() function in Python?

Leave a Reply

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