Introduction to Python Input String
In Python, to read the string or sentence from the user through an input device keyboard and return it to output screen or console we use input() function. In other languages also there are some built-in methods or functions to read the string on console and return it. In Python, as we know that we don’t specify any data types while declaring any variable, and also there are two different functions to display 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 accept the input line from the user there are two functions such as input() and raw_input().
Working of Python Input String
Python has two functions for taking in the input from the user or reads the data that is entered through the console and the result of this can be a string, list, tuple or int, etc which is stored into a variable. Two input functions of Python are:
This method of Python reads the input line or string and also 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 only when the user wants to read the data by entering through the console, and return value is a string.
This is optional, it can be any string or command which you want to display on the output console.
place = raw_input("Where do you stay?")
print ("Name of the place where I stay is: ", place)
This is also a Python method for reading the input string from the user through input device like keyboard and display the return value of this function on output screen or console. The return value of this method can be string or number or list or tuple, etc. In Python, we do not declare any data type while initializing or declaring the variable. As in raw_input() function, it can return only string value but in this input() function we can get the return value of any data type, Python decides as to what data type to be returned based on the variable value. Suppose if you have asked the user to enter the age then it will automatically take an int as the data type for the variable age.
prompt: This is optional again, it can be 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 function stops the execution of the further program until the user enters the value that is asked through this function.
name = input("Enter your name: ")
print("my name is: ", name)
age = input("Enter your age: ")
print("age is:" , age)
From the above program as the first variable “name” has input() function that asks the user to enter the name input() function always converts the value taken by into string as “name” variable is already of string type there is no such problem, but in the second variable it’s “age” which is of data type int, still the input() function converts it into string. Sometimes you can be more perfect by declaring input value as integer type explicitly.
Lets us discuss the Examples of Python Input String.
Let us take to display the multiplication of two numbers.
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)
The input() function can also accept the float value similarly to integer value by explicitly declaring float as we did it the above program as int.
fn1 = float(input("Enter float number: "))
print("The float number is: ", fn1)
This input() function can also be used to read the list as input from users. As we saw that we can accept int and float in the same way we can accept list and array also through this function().
lst = input ("Enter the list of numbers you want to display: ")
out_list = lst.split()
print(" The list you entered is: ", lst)
From the above example, we are using the split() function for splitting the string by space and appending those numbers to the list.
Lastly, you can use input() function to even read multiple values from users of different data types in one sentence. This can be shown in the below example:
Let us consider we want to ask the profile of the user as below code:
name, height, weight, age = input(" Enter the profile data as your name, height, weight, age \n using space to differentiate the values: ") .split()
print(" The profile data of the user is as follows: ", name, height, weight, age)
In Python also you can ask the user to enter the values or string or list of tuples, etc through any input device and display it on the output screen or console. As in C language, we do this by using scanf() function similarly in Python it can be done using raw_input and input() and to display these values obtained by these functions we use the print() function. As we saw in this article raw_input() function is very rarely used as it can return only the values of the string data type. But the input() function is mostly used as it can accept the input of any data type and if we want to be surer then we can even explicitly convert the value of the variable to the respective data type as input() function always converts any data type of the variable to string.
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 following articles to learn more –