In Python 2.x there are two functions for input. 
  1. input(): Takes expression as input. If you typed in 2+3 and print. It will show 5.
  2. raw_input(): Takes string as input. For above example, output will be '2+3'. However, we can change its data type later.
Python 3.x has eliminated input() and named raw_input() as input(). Basically it has only one function for taking data from user and that is input() explained below. 
(If you are using Python 2.x you can refer below description for raw_input(). It works the same.)

input( )

This function takes input as a string. If you want input in other formats you need to convert them to respective data types.

>>>input()

Type this in script mode and you'll see a cursor blinking in the next line. Input text is entered here. But where does it go?  You better do this!

>>>a = input()
Input string

Now, the input is stored in variable a. Let's print it and check if that's true.

>>>print a
Input string

Yeah. It works!
You can also use a prompt for input. Like this one:

>>>a = input('Enter a string: ')
Enter a string: Input string

What if you typed in numbers?

>>>b = input()
22
>>print b
'22'

The quotes on 22 signifies it is a string. But one must always be sure.

>>>print b+3
TypeError: cannot concatenate 'str' and 'int' objects

Hence proved. b is a string. Haha.

What if one want numbers as input. There's always a way. We'll see that soon after discussing data types in Python.
Happy weekend peeps!

0 Comments