Posted inPython

What is the Raw_input in Python?

Today, We want to share with you python raw_input.In this post we will show you Is there an alternative to input() in Python?, hear for python raw_input vs input we will give you demo and example for implement.In this post, we will learn about python extract data from json file with an example.

Python raw_input Example (Input From Keyboard)

raw_input syntax

for Python v2.x:

membersINFO = raw_input('Prompt :')
print (membersINFO)

Python v3.x as raw_input() was renamed to input() :

membersINFO = input('Prompt :')
print (membersINFO)

Python raw_input example

read the user name using raw_input() and display back on the screen using print():
Example 1:

a = raw_input('What\'s your name : ')
print 'Membername : ', a
What's your name : Virat Kohali
Membername :  Virat Kohali

What is it and Popular Commands in Python.

a string called option converted to a numeric variable:

#!/usr/bin/python
# Version 1
## Show menu ##
print (30 * '-')
print ("   M A I N - M E N U")
print (30 * '-')
print ("1. Sync")
print ("2. Member management")
print ("3. Reboot the server")
print (30 * '-')
 
## Get input ###
option = raw_input('Enter your option [1-3] : ')
 
### Convert string to int type ##
option = int(option)
 
### Take action as per selected menu-option ###
if option == 1:
        print ("Starting Syncing...")
elif option == 2:
        print ("Starting user management...")
elif option == 3:
        print ("Rebooting the server...")
else:    ## default ##
        print ("Invalid number. Try again...")

Other Way

#!/usr/bin/python
# Version 2
print (30 * '-')
print ("   M A I N - M E N U")
print (30 * '-')
print ("1. Sync")
print ("2. Member management")
print ("3. Reboot the server")
print (30 * '-')
 
###########################
## Robust error handling ##
## only accept int       ##
###########################
## Wait for valid input in while...not ###
is_valid=0
 
while not is_valid :
        try :
                option = int ( raw_input('Enter your option [1-3] : ') )
                is_valid = 1 ## set it to 1 to validate input and to terminate the while..not loop
        except ValueError, e :
                print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
 
### Take action as per selected menu-option ###
if option == 1:
        print ("Starting Syncing...")
elif option == 2:
        print ("Starting user management...")
elif option == 3:
        print ("Rebooting the server...")
else:
        print ("Invalid number. Try again...")

Sample outputs

------------------------------
   M A I N - M E N U
------------------------------
1. Sync
2. Member management
3. Reboot the server
------------------------------
Enter your option [1-3] : x
''x'' is not a valid integer.
Enter your option [1-3] : 
'''' is not a valid integer.
Enter your option [1-3] : 1
Starting Syncing...

Python 3 input() example

membersINFO = input('Prompt :')
print (membersINFO)

Convert String Value To Integer Value in Python 3

nano test.py

//Example
#!/usr/bin/python
# Version 3
number = int(input("What is your mobile number ? :"))
print(number)

last run this example python3 test.py

I hope you get an idea about How to obtain an user input?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype