Input Function in Python Programming


The input() function takes the user's input in the next single line, so whatever user writes in a signle line would be assign to to a variable. The input ( ) function helps to enter data at run time by the user and returns it as a string. This function prompts the user for an input from the keyboard.

Once the user has give the input and pressed enter One of the most important things to note here is that we're storing whatever the user entered into a variable. The output function print ( ) is used to display the result of the program on the screen after execution.

Use the int ( ) Function to Check if the Input Is an Integer in Python. The int ( ) function can convert a given string integer value to an integer type. Use the float ( ) Function to Check if the Input Is an decimal number in Python. The float ( ) function can convert a given string decimal number value to an float type.



The program takes input from the user in three different forms:

  • A string input, taken using the input() function, stored in the "name" variable, and its type and value are printed.
  • Two integer inputs, taken using the input() function and converted to integers using int(), stored in the variables "a" and "b", their sum is calculated and stored in "c", and the sum and type of "a" are printed.
  • Two float inputs, taken using the input() function and converted to float using float(), stored in the variables "a" and "b", their sum is calculated and stored in "c", and the sum and type of "a" are printed.

Source Code

Getting input in Python

#Getting String input Statement
name=input("Enter Name : ")
print(type(name))
print(name)

#Getting Integer input Statement
a=int(input("Enter The Value of A : "))
b=int(input("Enter The Value of B : "))
c=a+b
print(c)
print(type(a))

#Getting Float input Statement
a=float(input("Enter The Value of A : "))
b=float(input("Enter The Value of B : "))
c=a+b
print(c)
print(type(a))

Output

Enter Name : Tuttor
<class 'str'>
Tuttor
Enter The Value of A : 23
Enter The Value of B : 12
35
<class 'int'>
Enter The Value of A : 34.45
Enter The Value of B : 23.76
58.21000000000001
<class 'float'>

Multiple Values in Single Line

The program takes input from the user in the form of three names separated by either space or comma, stored in the variables name1, name2, name3. In the first input, the input() function is used to take the input, and the split() function is used to separate the names by space and store them in the respective variables. The values of the variables are then printed.

In the second input, the input() function is used to take the input, and the split() function is used to separate the names by a comma and store them in the respective variables. The values of the variables are then printed.

name1,name2,name3=input("Enter 3 Names : ").split()
print("Name 1 : ",name1)
print("Name 2 : ",name2)
print("Name 3 : ",name3)

name1,name2,name3=input("Enter 3 Names : ").split(',')
print("Name 1 : ",name1)
print("Name 2 : ",name2)
print("Name 3 : ",name3)

Output

Enter 3 Names : Ram kumar siva
Name 1 :  Ram
Name 2 :  kumar
Name 3 :  siva
Enter 3 Names : Ram kumar,Sam kumar,siva kumar
Name 1 :  Ram kumar
Name 2 :  Sam kumar
Name 3 :  siva kumar

Multiple Line String Input in Python

The program defines a string variable a containing a multi-line text string. The data type of the variable a is str. The program then uses the print function to display the data type of a, which is str, and the value of a which is the text string itself.

The program is a Python script that takes multi-line input from the user in the form of a paragraph.

  • A list "para" is initialized.
  • The user is prompted to "Enter a Para : "
  • The "while" loop is used to get multi-line input.
    • The line input is taken using the "input()" function and stored in the variable "line".
    • The "if" condition checks if the line is not empty, if it is not, the line is added to the list "para" using the "append()" method.
    • If the line is empty, the loop breaks.
  • The list "para" is printed.
  • The "join()" method is used to join the list "para" with a line break and store it in the variable "output".
  • The final output is printed.
a="""
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
"""
print(type(a))
print(a)


para=[]
print("Enter a Para : ")

while True:
  line=input()
  if line:
    para.append(line)
  else:
    break
print(para)
output='\n'.join(para)

print(output)

Output

<class 'str'>

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,

Enter a Para :
Ram is Good
HE is in Salem

['Ram is Good', 'HE is in Salem']
Ram is Good
HE is in Salem

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial