How to Use Python raw_input Function with Examples

Python is one of the easiest languages to learn and use, while at the same time being very powerful. Python is a general-purpose interpreted, interactive, object-oriented, and high-level
programming language.

The Python input() and raw_input() functions are used to read data from a standard input such as a keyboard. This article I will show how to use python raw_input function on python 2/python 3 versions with of examples.

Reading Input from Keyboard For Python 2

Python 2 has two versions of input functions, input() and raw_input().

The input() function treats the received data as string if it is included in quotes '' or "", otherwise the
data is treated as number.

    In Python 2

    >>> age = input('How old are you ?')
    How old are you ?: 30 # entered data is treated as number.
    >>> print age
    30

    >>> age = input('How old are you ? :')
    How old are you ? : '30' # entered data is treated as string.
    >>> print age
    '30'

The input('How old are you ?') function ask you about your age, You will inter integer value 30 from keyboard, This value will be stored in the age variable as integer. When we print the age variable with print age, The output is integer value 30.

On other hands, the input('How old are you ?') function ask you about your age, You will enter string '30' value from keyboard, This value will be stored in the age variable as string. When we print the age variable with print age function, The output is string value '30'.

The raw_input() function treats the received data as string even without quotes '' or "".

   In Python 2

   >>> age = raw_input("How old are you ? :")
   How old are you ? : 30 # entered data is treated as string even without ''
   >>> print age
   '30'

    >>> age = raw_input("How old are you ? :")
    How old are you ? : '30' # entered data treated as string including ''
    >>> print age
    "'30'"

The raw_input('How old are you ?') function ask you about your age, You will enter integer value 30 or string value '30' from keyboard, This value will be stored in the age variable as string. When we print the age variable with print age function, The output is string value '30' or "'30'".

Reading Input from Keyboard For Python 3

In Python 3, raw_input() function is deprecated. Further, the received data is always
treated as string.

    In Python 3

    >>> age = input("How old are you ? :")
    How old are you ? : 30
    >>> print(age)
    '30'

    >>> age = input("How old are you ? :")
    How old are you ? : '30' # entered data treated as string with or without ''
    >>> print(age)
    "'30'"

    >>> age = raw_input("How old are you ? :") # will result NameError
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    NameError: name 'raw_input' is not defined

The input('How old are you ?') function ask you about your age, You will enter integer value 30 or string value '30' from keyboard, This value will be stored in the age variable as string. When we print the age variable with print(age) function, The output is string value '30' or "'30'".

How To Convert String Value To Integer Value

To convert string value to integer value, We will use int() function.

    In Python 2

    >>> age = int(raw_input("How old are you ? :"))
    How old are you ? : 30
    >>> print(age)
    30

    >>> age = int(raw_input("How old are you ? :"))
    How old are you ? : '30'
    >>> print(age)
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    ValueError: invalid literal for int() with base 30: "'30'"
    In Python 3

    >>> age = int(input("How old are you ? :"))
    How old are you ? : 30
    >>> print(age)
    30

    >>> age = int(input("How old are you ? :"))
    How old are you ? : '30'
    >>> print(age)
    Traceback (most recent call last):
    File "<input>", line 1, in <module>
    ValueError: invalid literal for int() with base 30: "'30'"

When we enter digit value, The raw_input() function in python 2 and input() in python 3 stored it as string value. After that the int() function convert it to integer value. If we enter the digit value between quotes

How To Calculate The Area OF Circle

We will make a simple program to reading input from keyboard and how to execute it on Ubuntu 18.04, Calculation of circle area.

In Python 2

#!/usr/bin/python
import math
radius = int(raw_input("Please enter the radius of the circle : "))
area_of_circle = math.pi * radius**2
print area_of_circle

We will create file circle_area2.py and store the above code inside it.

[email protected]:~$ sudo nano circle_area2.py

To execute this program, We will type the command below.

[email protected]:~$ sudo python circle_area2.py
    In Python 3

    #!/usr/bin/python3
    import math
    radius = int(input("Please enter the radius of the circle : "))
    area_of_circle = math.pi * radius**2
    print(area_of_circle)

We will create file circle_area3.py and store the above code inside it.

[email protected]:~$ sudo nano circle_area3.py

To execute this program, We will type the command below.

[email protected]:~$ sudo python circle_area3.py

Read Also:

raw_input() and input() functions in python 2 and python 3 respectively is very important to receive the data from the users via keyboard. Also we must use int() function to convert the input string value to integer value.

Ahmed Abdalhamid 12:05 am

About Ahmed Abdalhamid

Technical Support Engineer experienced in Linux servers administration of production environments. Exploring DevOps culture and tools. Interested in containerization and open source monitoring tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

1 Comment

  1. Why "sudo" the final examples there? Should not be necessary to escalate to superuser privileges to run these simple python examples.