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 String startswith()
Python datetime
Python Dictionary fromkeys()
Python help()
Python Exception Handling Using try, except and finally statement
Python Set clear()
Python vars()
Python String expandtabs()
Python max()
Python Set intersection_update()
Python ord()
Python Program to Check If a String Is a Number (Float)
Python Program to Count the Number of Occurrence of a Character in String
Python Recursion
Python Dictionary values()
Python File I/O Operation
Python String rindex()
Python String format_map()
Python divmod()
Python Dictionary keys()
Python Program to Capitalize the First Character of a String
Python Program to Return Multiple Values From a Function
Python chr()
How to get current date and time in Python?
Python sorted()
Python String maketrans()
Python String isprintable()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Tuple index()
Python sum()
Python Operators
Python Program to Split a List Into Evenly Sized Chunks