command line input python code example

Example 1: input command in python shell

x = input("Input Your Name")
#lets say I input Bob
print(x)
#It should output Bob

Example 2: read argument from terminal

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

#Terminal
# $ python test.py arg1 arg2 arg3

#print
#Number of arguments: 4 arguments.
#Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

Example 3: python argument command line

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Example 4: inpuit inf terfminal ppython

userInput = input("This is a prompt for the input")

print userInput