Home » Python User Input

Python User Input

Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Oracle Java Certification
Spring Framework Basics Video Course

Python has a method that allows for user input. That means we are able to ask the user for input.

Depending on the version of Python you are using there are 1 of 2 methods available

Python 3.6 uses the input() method.

Python 2.7 uses the raw_input() method.

Table of Contents

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 incorrect then a syntax error or exception is raised by python.

 

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

Some notes about the input() method

When the input() function executes program flow will stop until the user has given an input.
The text or message on the output screen to ask a user to enter input value is completely optional
Whatever you enter as an input value the input function will convert it to a string. You will need to convert it into an integer in your code using typecasting if you wanted to use an integer value.

raw_input ( )

This function works in older versions of Python (such as Python 2.7).

This function takes exactly what is typed from the keyboard, converts it to a string and then return it to the variable in which we want to store.

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

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More