Best Laptop
Python Tutorial 6 – Use of Input() function

Python Tutorial 6 – Use of Input() function

Today we will study about the use of Python input function – interacting with computer. This function will enable you to get some data from computer. In a way, print function sends data to the console and input function gets the data from console. The data received via input function can be used in the program for further calculations.

  • The data received via input function always carry string data type. To treat the data as integer or float, we need to change the data type
  • Data received via input function should always be first assigned to variable after which the data can be used using that variable

1. Receive data using Python input function

var = input()

This is the most basic way of using the input function. The first statement asks the user to input some data. Once the data is entered, it is assigned to var variable. Second statemen prints the var value

2. Make input function to print a message also

str = input(“Enter some data: “)
print(“Entered data is: ”, str)

Here on the console you will see a message to “Enter some data: “ where you will enter something. Next statement prints the data along with statement “Entered data is: “

3. As we already saw, data from input function is always treated as string. How can be change that to integer or float:

num = int(input(“Enter a number: “))
print(“Sum is: “, num+2)
num1 = float(input(“Enter a number: “))
print(“Sum is: “, num+2)

Here we are just casting the value entered into an integer. And then print function just performs an addition and displays it. Remember in this case that while entering data, if we enter a string value, then we will see error because string cannot be casted to integer

4. Treating data as integer without casting will give error:

num =   input(“Enter a number: “)
print(“Sum is: “, num+2)

This will throw error because we are not converting a string to integer. Even if the entered data is an integer value, still input will treat that as a string. Hence, we must remember to do the conversion accordingly.

Use of arithmetic operator + and * on strings: As we have seen earlier on Python Operators session, + sign concatenates the strings while * operator multiplies the strings.

5. Use of + on strings:

str1 = input(“Enter first string”)
str2 = input(“Enter second string”)
print(str1 + str2)
print(str2 + str1)

Try this example, you will see that the print statements concatenate the strings. Remember that both the print statements will give different output here since addition on strings is not commutative. When addition is used on integers, then addition is commutative: 2+3 is same as 3+2 but in case of string

6. Use of * on strings:

str1 = input(“Enter string”)
print(str1 * 5)

Here value of str1 will be displayed 5 times

7. We can convert integer into string also like below. Try out these examples and check the results.


Commenting some statement in Python:

1. We can comment some statement in Python using # symbol

print(“This will be displayed”)
#print(“This is commented”)
Comment multiple statement
#print(“First comment”)
print(“Second comment”)#
print(“display this”)
Keyboard short cut for putting comment is CTRL + /

Previous Session:

Leave a Reply

Your email address will not be published. Required fields are marked *

Python 3.11.1 Features