OSETC TECH

How to Get Input from Keyboard in Python

This post will guide you how to get an user input from keyboard with raw_input function or input function in python language. How do I use raw_input or input function to get input from keyboard in Python.

Both input() and raw_input() functions can be used to read data or input from a standard input such as a keyboard in python. And the raw_input function is only valid in python2 version. And the input() function is available in both python2 and python3.

Get Input from Keyboard in Python2


you can type the following python command at the shell prompt in your Linux system, and it will enter into python2 interpreter.

[root@mydevops opc]# python
Python 2.7.5 (default, Jun 20 2019, 20:27:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> name=input("what's your name?")
what's your name?"osetc.com"
>>> print(name)
osetc.com
>>>

>>> name=raw_input("what's your name?")
what's your name?"osetc.com"
>>> print(name)
"osetc.com"
>>>

Get Input from Keyboard in Python3


if you are using python3 interpreter in your Linux server, and you can only use input() function to get the input data from your keyboard. So you can type the following python3 command to enter into python3’s interpreter. type:

$ python3

devops@devops:~$ python3
Python 3.6.8 (default, Aug 20 2019, 17:12:48)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name=input("what's your name?")
what's your name?"osetc.com"
>>> print(name)
"osetc.com"
>>> name=raw_input("what's your name?")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>>