Tutorialskeyboard_arrow_down
Studentkeyboard_arrow_down
Courses
Jobskeyboard_arrow_down
Sign In
Sign In
Home
Courses
Algorithmskeyboard_arrow_down
Data Structureskeyboard_arrow_down
Languageskeyboard_arrow_down
Interview Cornerkeyboard_arrow_down
GATEkeyboard_arrow_down
CS Subjectskeyboard_arrow_down
Studentkeyboard_arrow_down
Jobskeyboard_arrow_down
GBlog
Puzzles
What's New ?
Introductionexpand_more
Input/Outputexpand_more
o Taking input in Python
o Taking input from console in Python
o Taking multiple inputs from user in Python
o Python | Output using print() function
o How to print without newline in Python?
o Python end parameter in print()
o Python | sep parameter in print()
o Python | Output Formatting
Operatorsexpand_more
Data Typesexpand_more
Control Flowexpand_more
Functionsexpand_more
Python OOPexpand_more
Exception Handlingexpand_more
File handlingexpand_more
Python Regexexpand_more
Python Collectionsexpand_more
Python NumPyexpand_more
Python Pandasexpand_more
Python Djangoexpand_more
Python JSONexpand_more
Python CSVexpand_more
Python MySQLexpand_more
Python MongoDBexpand_more
Python OpenCVexpand_more
Python Seleniumexpand_more
Python Tkinterexpand_more
Python Kivyexpand_more
Python Examples and Quizexpand_more
Taking input in Python
Last Updated: 05-08-2020
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 then evaluates the expression, which means
Python automatically identifies whether user entered a string or a number or list. If the input provided is not
correct then either syntax error or exception is raised by python. For example –
filter_none
edit
play_arrow
brightness_4
# Python program showing
# a use of input()
val = input("Enter your value: ")
print(val)
Output:
How the input function works in Python :
When input() function executes program flow will be stopped until the user has given an input.
The text or message display on the output screen to ask a user to enter input value is optional i.e.
the prompt, will be printed on the screen is optional.
Whatever you enter as input, input function convert it into a string. if you enter an integer value
still input() function convert it into a string. You need to explicitly convert it into an integer in
your code using typecasting.
Code:
filter_none
edit
play_arrow
brightness_4
# 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, convert it to string and then return it to the variable in which we want to store. For
example –
filter_none
edit
play_arrow
brightness_4
# Python program showing
# a use of raw_input()
g = raw_input("Enter your name : ")
print g
Output :
Here, g is a variable which will get the string value, typed by user during the execution of 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.
Refer to the article Taking list as input from the user for more information.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn
the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python
DS Course.
Recommended Posts:
Taking input from console in Python
Taking multiple inputs from user in Python
Python VLC MediaPlayer – Taking Screenshot
Taking Screenshots using pyscreenshot in Python
Python Input Methods for Competitive Programming
Vulnerability in input() function – Python 2.x
Generate two output strings depending upon occurrence of character in input string in Python
Python | Find all close matches of input string from a list
Python | Get a list as input from user
Take Matrix input from user in Python
Python regex | Check whether the input is Floating point number or not
Python | Accepting Script Input
Python | Categorizing input Data in Lists
PyQt5 Input Dialog | Python
Difference between input() and raw_input() functions in Python
Python program to input a comma separated string
fileinput.input() in Python
Take input from stdin in Python
How to take integer input in Python?
Python 3 - input() function
ABHISHEK TIWARI 13
Check out this Author's contributed articles.
If you like GeeksforGeeks and would like to contribute, you can also write an article
using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article
appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Article Tags :
Python
python-basics
python-input-output
thumb_up
62
To-do Done
1.4
Based on 33 vote(s)
Improve Article
Please write to us at [email protected] to report any issue with the above content.
Post navigation
Previous
first_page Create pandas dataframe from lists using dictionary
Next
last_pagePython | Calendar Module
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
Most popular in Python
Python map() function
Adding new column to existing DataFrame in Pandas
Iterate over a list in Python
Python program to convert a list to string
Read a file line by line in Python
More related articles in Python
Enumerate() in Python
How to get column names in Pandas dataframe
Python String | replace()
Reading and Writing to text files in Python
append() and extend() in Python
room5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305
[email protected]
Company
About Us
Careers
Privacy Policy
Contact Us
Learn
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
Practice
Courses
Company-wise
Topic-wise
How to begin?
Contribute
Write an Article
Write Interview Experience
Internships
Videos
@geeksforgeeks , Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you
have read and understood our Cookie Policy & Privacy PolicyGot It !