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 Set issuperset()
Python Program to Find the Factorial of a Number
Python Program to Find the Sum of Natural Numbers
Python String rsplit()
Python Program to Sort a Dictionary by Value
Python Functions
Python Program to Merge Mails
Python datetime
Python Set clear()
Python Program to Find the Factors of a Number
Python Program to Get the Last Element of the List
Python String format_map()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python ord()
Python tuple()
Python Program to Calculate the Area of a Triangle
Python Program to Add Two Numbers
Python Program to Print all Prime Numbers in an Interval
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String isalnum()
Python Program to Represent enum
Python vars()
Python Program to Remove Duplicate Element From a List
Python Program to Find Factorial of Number Using Recursion
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Check Leap Year
Python issubclass()
Python dict()
Python String rfind()
Python staticmethod()
Python Program to Convert Decimal to Binary Using Recursion
Deep Learning with Python - Francois Chollet