DEV Community

Cover image for Python Basics 4: Input Function
Irfan Faisal
Irfan Faisal

Posted on

Python Basics 4: Input Function

Input Function:
For taking input from the user directly, Python has a special built-in function, input(). It takes a string as a prompt argument, and it is optional. It is used to display information or messages to the user regarding the input.

syntax: input("A prompt for the user")

Suppose you're building a program where you need to know the name of the user. You can use the input function to do that.
name=input("What's your name?")

Inside the input, we write the prompt, which will be displayed in the terminal to the user. Then user can write their input.

input() pauses program execution to allow the user to type in a line of input from the keyboard. After typing, the user needs to press the ENTER key otherwise, the program will be waiting for the user's input indefinitely.

Once the user presses the Enter key, all characters typed are read, stored in the variable (name), and returned as a string.
(remember this, we'll talk about type conversion briefly!)

Now, let's suppose you also want to ask the user's age. As usual, you wrote your program like this:
age=input("Enter your age:")

If your program is executed like this, at first glance, you won't see any problems with the code. However, the problem will arise when you want to do calculations with this age.

age=input("Enter your age:")
print(age+10)

#TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

Why did this error occur? So technically, your age variable never stored an integer in the first place.

print(type(age)) #output: <class 'str'>
As you can see your age variable has stored the input as a string. And you can't do calculations between string and integer.

In order to solve the problem, you need to convert your string variable into integer. Which is called Type Conversion or Casting.

How can you do that?
First, write down your datatype that you want to convert into. Then, use your input function to take input from the user.

age=int(input("Enter your age"))

This way, now your age variable will convert string into integer and store that. And now you can do calculations.

More Examples: float()- to convert into float, str()=to convert into string etc.

This type of type conversion is known as ;Explicit Casting, which is done by us, the programmars.

Implicit Casting: It is done by python. Here, python automatically changes between data types.

Ex:

x=10(Integer)
x+=10.5
Print(x) >>> 20.5(Float)
Print(type(x)) >>> <class 'float'>

Enter fullscreen mode Exit fullscreen mode

Here, python automatically changed integer into float .

The input function is one of the most used functions in python. You can make your program more user-oriented by using input function.

Top comments (1)

Collapse
 
coderanger08 profile image
Irfan Faisal

Here is my next blog in the python basics series. If you have any questions or feedback, let me know in the comments.