Saturday, 5 December 2015

Python : Reading input

If you are using Python 2.X, raw_input is used to read input from user.

raw_input([prompt])
Argument prompt is optional. If the prompt argument is present, it is written to standard output without a trailing newline. 'raw_input' function reads input from user and return the input in string format.

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name=raw_input("Enter your name\n")
Enter your name
Hari Krishna
>>> print name
Hari Krishna

‘raw_input” is deprecated in Python3.x. You have to use input method to read input.

input([prompt])
Argument prompt is optional. If the prompt argument is present, it is written to standard output without a trailing newline. 'input' function reads input from user and return the input in string format

$ python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> name=input("Enter your name\n")
Enter your name
Hari Krishna
>>> 
>>> print(name)
Hari Krishna




Previous                                                 Next                                                 Home

No comments:

Post a Comment