Accept user input in Python 3 | User Input/output in Python 3

We have already seen and utilized the print() function  through many examples. It is utilized for displaying data, by default, on the output screen. However, in some cases we might need to further modify the output or need to receive specific input from the user to customize the Python program according to our needs. Entering input data into a program and extracting output data is important in programming. Without user-given input, programs would be much less flexible and more static , except when they themselves generate random values. The output allows you to see, use, pass on the result of the program’s processing.

Typically, a program is required to process a range of different inputs that come from external sources. The latter can be files, keyboard, network, output from another program. Output processing results can be similarly saved to various different files if needed.

Accept user input in Python 3 | Input() function in Python 3

Input() function is responsible for entering data into a program from the keyboard in Python input( ) . When this function is called, the program stops executing and waits for the user to enter text. After that, when the user hits Enter, the function input( ) will take the entered text and pass it on to the program, which will already process it according to its algorithms. The function input( ) transfers the entered data to the program. They can be assigned to a variable. 

firstname = input("Enter first name: ")
lastname = input("Enter last name: ")
print("Fullname  is: " + firstname + " " + lastname)
Enter first name: Ted
Enter last name: Martez
Fullname  is: Ted Martez

When running the program, the computer waits for one line to be entered first, then the second. They will be assigned to the variables ‘firstname’ and ‘lastname’. The values ​​of these variables are then displayed using formatted output.

By default the input given value is assumed to be a string, however we might need to accept various different kinds of value from the user depending upon the nature of the program. Hence, to accept numeric values we can utilize either the int() and float() functions to convert the given input into those data types.

numOranges =  int ( input ( "How many oranges do you want to buy? " ) ) 
costOrange =  float ( input ( "What is the cost of a single orange? " ) )
sumOranges = numOranges * costOrange
print ( "Pay", sumOranges,  "Rupees" )
How many oranges do you want to buy? 45
What is the cost of a single orange? 30
Pay 1350.0 Rupees

Print in Python | Print() function

While the default print() function is sufficient in most cases, we can specify additional parameters or conditions to modify the output of the print() statement. Using a parameter, sep you can specify a line separator other than a space:

#Specifies to separate the output elements with "-"
print ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", sep = "-")

#Specifies to separate the output elements with "//"
print (1, 2, 3, sep = "//")

The end parameter allows you to specify what to do after the line is output. By default, a line break occurs. However, this action can be modified by specifying any other character or string:

#Specifies to end the print statement with "n"
print (10, " ", end = "n")
10  n

Formatting the output of print statement

The format( ) method formats the specified value(s) and insert them inside the string’s placeholder. The placeholder is defined using curly brackets: {}

# The number values within the curly brackets will be replaced with the respective values specified in the format method according to order.
print ("This is a {0}. It's {1}.".format ("ball", "red"))
print ("This is a {0}. It's {1}.".format ("cat", "white"))
print ("This is a {0}. It's {1} {2}.".format (1, "a", "number"))
This is a ball. It's red.
This is a cat. It's white.
This is a 1. It's a number.

In the above example, the values are substituted for the format characters/words.

The first argument of the method will be substituted for the zero place format( ), the second one for the place with the number 1, and so on. In fact, the possibilities of the format( ) method are much wider, and a separate lesson would be needed to study them. This will be enough for us for now.

In the next article we will look at various FIle operations in Python.

Leave a Comment