Open In App

Taking input in Python

Last Updated : 19 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. 
 

  • input ( prompt )
  • raw_input ( prompt )


input (): This function first takes the input from the user and converts it into a string. The type of the returned object always will be <class ‘str’>. It does not evaluate the expression it just returns the complete statement as String. For example, Python provides a built-in function called input which takes the input from the user. When the input function is called it stops the program and waits for the user’s input. When the user presses enter, the program resumes and returns what the user typed. 

Syntax:

inp = input('STATEMENT')

Example:
1. >>> name = input('What is your name?\n') # \n ---> newline ---> It causes a line break
>>> What is your name?
Ram
>>> print(name)
Ram

# ---> comment in python
Python
# Python program showing 
# a use of input()

val = input("Enter your value: ")
print(val)

Output:

 

Taking String as an input:

Python
name = input('What is your name?\n')     # \n ---> newline  ---> It causes a line break
print(name) 

Output:

What is your name?
Ram
Ram

How the input function works in Python : 
 

  • When input() function executes program flow will be stopped until the user has given input.
  • The text or message displayed on the output screen to ask a user to enter an input value is optional i.e. the prompt, which will be printed on the screen is optional.
  • Whatever you enter as input, the input function converts it into a string. if you enter an integer value still input() function converts it into a string. You need to explicitly convert it into an integer in your code using typecasting. 

Code: 

Python
# Program to check input 
# type in Python

num = input ("Enter number :")
print(num)
name1 = input("Enter name : ")
print(name1)

# Printing type of input value
print ("type of number", type(num))
print ("type of name", type(name1))

Output: 

raw_input(): This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, converts it to string, and then returns it to the variable in which we want to store it.

Example:

Python
# Python program showing
# a use of raw_input()

g = raw_input("Enter your name : ")
print g

Output:

 
Here, g is a variable that will get the string value, typed by the user during the execution of the program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case, we use typecasting. For more details on typecasting refer this. 

Note: input() function takes all the input as a string only

There are various function that are used to take as desired input few of them are : –

  • int(input())
  • float(input())
Python
num = int(input("Enter a number: "))
print(num, " ", type(num))

          
floatNum = float(input("Enter a decimal number: "))
print(floatNum, " ", type(floatNum))

                 

Output: 

Output

Output

Refer to the article Taking list as input from the user for more information. 

Taking Input in Python – FAQs

How to input a number in Python?

You can use the input() function to accept user input. Convert the input to an integer or float using int() or float() if necessary.

# Input a number
num = input("Enter a number: ")
# Convert input to integer
num = int(num)

How to take character input in Python?

You can use the input() function to accept user input. Characters are inherently strings in Python, so no additional conversion is necessary.

# Input a character
char = input("Enter a character: ")

How to check input type in Python?

You can use the type() function to check the type of input received from input().

user_input = input("Enter something: ")
print(type(user_input))

How to print input value in Python?

Simply use the print() function to display the input value.

user_input = input("Enter something: ")
print("You entered:", user_input)

How to input time in Python?

You can use the input() function to accept a string input representing time. You can then parse and manipulate this string using datetime functions if needed.

import datetime
# Input time as string
time_str = input("Enter a time (HH:MM:SS): ")
# Convert string to datetime object
time_obj = datetime.datetime.strptime(time_str, "%H:%M:%S")


Previous Article
Next Article

Similar Reads

Taking input from console in Python
What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks like this. Here we write a command and to execute
2 min read
Python VLC MediaPlayer – Taking Screenshot
In this article we will see how we can take screen shot of the MediaPlayer object in the python vlc module. VLC media player is a free and open-source portable cross-platform media player software and streaming media server developed by the VideoLAN project. MediPlyer object is the basic object in vlc module for playing the video. A screenshot, als
2 min read
Taking Screenshots using pyscreenshot in Python
Python offers multiple libraries to ease our work. Here we will learn how to take a screenshot using Python. Python provides a module called pyscreenshot for this task. It is only a pure Python wrapper, a thin layer over existing backends. Performance and interactivity are not important for this library. Installation Install the package pyscreensho
2 min read
Taking multiple inputs from user in Python
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.  Methods to Take multiple inputs from user Using split() method :Using map() with split():Using List comprehension : Using s
4 min read
Generate two output strings depending upon occurrence of character in input string in Python
Given an input string str[], generate two output strings. One of which consists of that character that occurs only once in the input string and the second consists of multi-time occurring characters. Output strings must be sorted. Examples: Input : str = "geeksforgeeks" Output : String with characters occurring once: "for". String with characters o
4 min read
Python | Find all close matches of input string from a list
We are given a list of pattern strings and a single input string. We need to find all possible close good enough matches of input string into list of pattern strings. Examples: Input : patterns = ['ape', 'apple', 'peach', 'puppy'], input = 'appel' Output : ['apple', 'ape'] We can solve this problem in python quickly using in built function difflib.
2 min read
Get a list as input from user in Python
We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to get input a list from the user using Python. Example: Input : n = 4, ele = 1 2 3 4Output : [1, 2, 3, 4]Input : n = 6, ele = 3 4 1 7 9 6Output : [3, 4, 1, 7, 9, 6]Get a list as input from user in Python using Loop C/C++ Cod
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Python regex | Check whether the input is Floating point number or not
Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Output: Not a Floating point number In this program, we
2 min read
Compute the square root of negative input with emath in Python
In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy. Example:Input: [-3,-4] Output: [0.+1.73205081j 0.+2.j ] Explanation: Square root of a negative input.NumPy.emath.sqrt method: The np.emath.sqrt() method from the NumPy library calculates the square root of complex inputs. A complex val
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg