Read Three Integers From User Input Python 3
Introduction to Python Input String
In Python, to read the cord or sentence from the user through an input device keyboard and render it to the output screen or console, nosotros use the input() function. In that location are also some born methods or functions to read the cord on the console and render it in other languages. In Python, as we know that we don't specify whatever information types while declaring any variable, and besides there are two dissimilar functions to brandish and handle input data obtained from the user, they are input() and print(). In Python, the print() function is used to display the input obtained or the value it wants to display, which is specified, whereas to take the input line from the user, at that place are two functions such equally input() and raw_input().
Working of Python Input String
Python has two functions for taking in the input from the user or reads the information that is entered through the console, and the result of this can be a string, listing, tuple or int, etc., which is stored into a variable. Two input functions of Python are:
1. raw_input()
This Python method reads the input line or string and can read commands from users or data entered using the console. The return value of this method will be only of the string data type. In Python, this method is used just when the user wants to read the data by entering through the panel, and the return value is a string.
Syntax:
raw_input([prompt])
Parameter:
prompt:
This is optional; it can exist any string or control which you want to display on the output console.
Example:
place = raw_input("Where exercise you lot stay?")
impress ("Name of the place where I stay is: ", identify)
print(type(identify))
Output:
ii. input()
This is also a Python method for reading the input string from the user through an input device like a keyboard and brandish the return value of this function on the output screen or console. The render value of this method tin can be string or number or list or tuple, etc. In Python, we practise non declare any information type while initializing or declaring the variable. Equally in the raw_input() function, it can render an just a string value, simply in this input() office, nosotros can get the return value of any information type; Python decides as to what data type to be returned based on the variable value. If you have asked the user to enter the age, it will automatically accept an int as the data type for the variable age.
Syntax:
input([prompt])
Parameter:
prompt: This is optional once again; information technology can exist any string or commands that the user wants to display. In this function, whatever the user enters, the input() function converts it into a string, and this part stops the execution of the further program until the user enters the value that is asked through this function.
Example:
name = input("Enter your name: ")
print("my name is: ", name)
age = input("Enter your age: ")
print("age is:" , historic period)
print(type(name))
print(type(historic period))
Output:
From the above plan, as the beginning variable "name" has input() function that asks the user to enter the proper name input() function always converts the value taken by into string as "name" variable is already of string type in that location is no such problem, but in the 2nd variable it's "age" which is of information type int, withal the input() function converts information technology into a string. Sometimes you lot can be more perfect by declaring input value every bit integer type explicitly.
Examples
Lets united states discuss the Examples of Python Input String.
Example #1
Permit the states have to brandish the multiplication of two numbers.
Code:
n1 = int( input("Enter the first number for multiplication: "))
n2 = int( input("Enter the second number for multiplication: "))
product = n1 * n2
print( "Multiplication of two numbers is: ", product)
Output:
Example #two
The input() function tin as well accept the float value similarly to integer value by explicitly declaring float equally we did in the in a higher place plan as int.
Lawmaking:
fn1 = float(input("Enter bladder number: "))
print("The bladder number is: ", fn1)
Output:
Instance #3
This input() part can besides be used to read the listing as input from users. We could accept int and float in the aforementioned mode we can take listing and array besides through this function().
Code:
lst = input ("Enter the listing of numbers you want to display: ")
out_list = lst.split up()
print(" The list you entered is: ", lst)
Output:
Example #four
From the above case, we are using the carve up() function to split the string by space and suspend those numbers to the list.
Lastly, you can utilize the input() function to even read multiple values from users of different data types in ane sentence. This can be shown in the below example:
Let united states consider we desire to ask the profile of the user equally below code:
Code:
name, height, weight, age = input(" Enter the profile data as your name, pinnacle, weight, historic period \n using space to differentiate the values: ") .carve up()
print(" The profile information of the user is equally follows: ", name, top, weight, age)
Output:
Conclusion
In Python also y'all tin ask the user to enter the values or string or listing of tuples, etc., through an input device and display it on the output screen or panel. Equally in C language, we exercise this by using the scanf() office similarly in Python, it can be done using raw_input and input(), and to display these values obtained by these functions, nosotros employ the impress() role. As nosotros saw in this article raw_input() function is very rarely used as it tin can return only the values of the string data type. Just the input() function is generally used every bit information technology tin can have the input of whatever data type and if we want to be surer, then nosotros can even explicitly catechumen the value of the variable to the respective data type as input() function always converts any information type of the variable to cord.
Recommended Articles
This is a guide to Python Input String. Here we also discuss the Introduction and syntax of Python Input String along with different examples and code implementation. You may also have a look at the post-obit articles to larn more –
- Python Counter
- Python Concurrency
- Python argparse
- Python Individual Variables
Source: https://www.educba.com/python-input-string/
Enviar um comentário for "Read Three Integers From User Input Python 3"