Blog

03/06/2019

Python raw_input Example (Input From Keyboard)



I would like to read data from the user using Python. How do I read data using raw_input()? Can you provide Python raw_input() examples?

The raw_input() function reads a line from input (i.e. the user) and returns a string by stripping a trailing newline.

This page shows some common and useful raw_input() examples for new users. Please note that raw_input() was renamed to input() in Python version 3.x. See below for more info.

raw_input syntax

The syntax is as follows for Python v2.x:

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

The syntax is as follows for Python v3.x as raw_input() was renamed to input() :

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

In the above example, a string called mydata stores users data. Please note that if you want to compare mydata, then convert mydata to a numeric variable using int().

Python raw_input() examples

In this example, read the user name using raw_input() and display back on the screen using print():

#!/usr/bin/python
name=raw_input('Enter your name : ')
print ("Hi %s, Let us be friends!" % name);

Sample outputs:

Enter your name : nixCraft
Hi nixCraft, Let us be friends!

In this following example, a string called choice 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. Backup")
print ("2. User management")
print ("3. Reboot the server")
print (30 * '-')
 
## Get input ###
choice = raw_input('Enter your choice [1-3] : ')
 
### Convert string to int type ##
choice = int(choice)
 
### Take action as per selected menu-option ###
if choice == 1:
        print ("Starting backup...")
elif choice == 2:
        print ("Starting user management...")
elif choice == 3:
        print ("Rebooting the server...")
else:    ## default ##
        print ("Invalid number. Try again...")

OR

#!/usr/bin/python
# Version 2
print (30 * '-')
print ("   M A I N - M E N U")
print (30 * '-')
print ("1. Backup")
print ("2. User 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 :
                choice = int ( raw_input('Enter your choice [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 choice == 1:
        print ("Starting backup...")
elif choice == 2:
        print ("Starting user management...")
elif choice == 3:
        print ("Rebooting the server...")
else:
        print ("Invalid number. Try again...")

Sample outputs (note down invalid inputs are detected on fly):

------------------------------
   M A I N - M E N U
------------------------------
1. Backup
2. User management
3. Reboot the server
------------------------------
Enter your choice [1-3] : x
''x'' is not a valid integer.
Enter your choice [1-3] : 
'''' is not a valid integer.
Enter your choice [1-3] : 1
Starting backup...

Python 3 input() example

In Python version 3.x and above you need to use the input() not raw_input(). The syntax is:

mydata = input('Prompt :')
print (mydata)
Python 3 input() demo

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...