piton

Python User Input

Python User Input

Python is a flexible, efficient, and easy-to-learn programming language that provides a complete way and the liberty to build dynamic systems. Often, developers need to interact with users. The user might enter some data that is used for processing and calculation purposes. For example, for writing a program in Python that calculates the sum of two values, the user enters the values and the program returns the sum value as an output. In this case, it is necessary to take an input from the user to calculate the sum.

Python allows you to take the input from the user. Python provides two built-in functions for taking inputs from users:

  1. input ()
  2. raw_input ()

In Python 3.6, the input () function is used to take input from users, whereas, in Python 2.7, the raw_input () function is used to take input from users. The examples shown in this article use Python 3.6, and the Spyder3 editor is used for creating and writing the Python scripts.

First, we will discuss the input () function.

Using the input () Function

This section covers the syntax of the input () function. The following is the syntax of the input () function:

input (prompt)

The 'prompt' is a string that is displayed on the console that asks the user to enter the value in response. The user-entered input value is then stored into a variable, as follows:

name = input(“Enter your name”)

Whichever name value that the user enters will be stored in the 'name' variable. For example:

# user entering the name value
name = input ("Enter your name: ")
#printing the username
print("\nThe username is:",name)

Output

The output is displayed on the Python console.

The user input value is always converted into a string, no matter whether the user enters an integer value, float value, etc. The type () function can be used to determine the type of the user-entered value. The input is taken from the user and the type of the user-entered value will always be a string. Let us see an example of this.

# user entering the name value
value= input ("Enter a string value: ")
#printing the type of value
print("The type of ",value," is", type(value))
# user entering the int value
num= input ("Enter an integer value: ")
#printing the type of num
print("The type of ",num," is", type(num))
# user entering the float value
float_num= input ("Enter an float value: ")
#printing the type of float number
print("The type of ",float_num," is", type(float_num))
# user entering the complex number
complex_num= input ("Enter a complex number: ")
#printing the type of complex number
print("The type of ",complex_num," is", type(complex_num))

Output

The output is displayed in the Python console. In the given output, you can see that the type of every value entered is a string. It does not matter whether the user enters a string value, integer value, float value, or complex number; the type of the user-entered value will always be a string.

Convert User Input into Other Data Types

Although we cannot get an integer, float, or complex number as an input from the user, we can convert the user input value into other above-mentioned data types. For example:

# user entering the name value
value= input ("Enter a string value: ")
#printing the type of value
print("The type of ",value," is", type(value))
# user entering the int value
num= input ("Enter an integer value: ")
#convrting the value into an integer
num=int(num)
#printing the type of num
print("The type of ",num," is", type(num))
# user entering the float value
float_num= input ("Enter an float value: ")
#convrting the value into a floating point number
float_num=float(float_num)
#printing the type of float number
print("The type of ",float_num," is", type(float_num))
# user entering the complex number
complex_num= input ("Enter a complex number: ")
#convrting the value into a complex number
complex_num=complex(complex_num)
#printing the type of complex number
print("The type of ",complex_num," is", type(complex_num))

Output

The output is displayed in the Python console. In the output below, it can be seen that the type of the integer, floating point number, and complex number have now changed.

Conclusion

In this article, you learned about Python user inputs. It is easy to take an input from users in Python. The user-entered value is a string, but you can easily change the data type of the user-entered value. The data type conversion mechanism is briefly explained in the examples provided in this article.

Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...
Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...
AppyMouse On-screen Trackpad and Mouse Pointer for Windows Tablets
Tablet users often miss the mouse pointer, especially when they are habitual to using the laptops. The touchscreen Smartphones and tablets come with m...