Table of Contents
In this tutorial, we will learn about the Python input() function with the help of examples.
The input()
method takes input from the user and returns it.
Example
name = input("Enter your name: ") print(name) # Output: Enter your name: "Sheeran" # Sheeran
1. input() Syntax
The syntax of input()
method is:
input([prompt])
2. input() Parameters
The input()
method takes a single optional argument:
- prompt (Optional) – a string that is written to standard output (usually screen) without trailing newline
3. input() Return Value
The input()
method reads a line from the input (usually from the user), converts the line into a string by removing the trailing newline, and returns it.
If EOF is read, it raises an EOFError
exception.
4. Example 1: How input() works in Python?
# get input from user inputString = input() print('The inputted string is:', inputString)
Output
Python is interesting. The inputted string is: Python is interesting
5. Example 2: Get input from user with a prompt
# get input from user inputString = input('Enter a string:') print('The inputted string is:', inputString)
Output
Enter a string: Python is interesting. The inputted string is: Python is interesting
Related posts:
Python eval()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Check if a Number is Odd or Even
Python Set intersection_update()
Python Program to Display Calendar
Python String maketrans()
Python sorted()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python ascii()
Python String expandtabs()
Python Program to Capitalize the First Character of a String
Python Program to Sort Words in Alphabetic Order
Python complex()
Python list()
Python String encode()
Python String center()
Python while Loop
Python Program to Parse a String to a Float or Int
Python len()
APIs in Node.js vs Python - A Comparison
Python all()
Python Global, Local and Nonlocal variables
Python any()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Intelligent Projects Using Python - Santanu Pattanayak
Python sum()
Python Function Arguments
Python Program to Add Two Numbers
Python compile()
Python String index()
Python Program to Convert String to Datetime
How to get current date and time in Python?