How to apply the input() function in Python

The input function in Python can be used to gather information from users.

You may use the following syntax to apply the input function in Python:

value = input()

Next, you’ll see 3 examples of an input function:

  1. For a text/string
  2. Numeric values
  3. Summing numeric values

Additionally, you’ll see how to use the tkinter package to gather input from users.

3 Examples of an Input Function in Python

Example 1: input function for a text/string

You can utilize the input function in order to gather text from users.

For example, the following syntax can be used to gather the name of an individual user:

print('What is your name?')
value = input()
print('Your name is: ' + value)

Run the code, and you’ll see this question:

Question

Enter your name (for example, enter ‘Jon’), and then press ENTER:

Input by users in Python

You’ll then see the name that you entered:

How to apply the input() function in Python

Example 2: input function for numeric values

Similarly, you can gather numeric values from users.

Here is the code to gather the age of an individual:

print('What is your age?')
value = input()
print('Your age is: ' + value)

Once you run the code, you’ll see the following question:

Question

Enter an age (for instance, enter the age of 27), and then press ENTER:

How to apply the input function in Python

You’ll then see the age that you entered:

Input function in Python

Example 3: summing numeric values

What if you want to perform some arithmetic calculations once you gathered a numeric value from a user?

For example, what if you want to add an additional 5 years to the original age entered by the user?

In that case, make sure to apply an int() around the input function for integers values:

value = int(input())

This will allow you to work with integers, and thus avoid the following error (when summing values for example):

TypeError: can only concatenate str (not “int”) to str

Also, when printing the results, make sure that everything is set to strings (i.e., by applying str() when printing):

print('What is your age?')
value = int(input())
value_in_five_years = value + 5
print('In 5 years from now you will be: ' + str(value_in_five_years))

Run the code and you’ll see this question:

Example of question

Type an age (for example type 27), and then press ENTER:

Numeric values - input function in Python

You’ll then get the age 5 years from now:

How to apply the input function in Python

Using Tkinter to Gather Input from Users

You can also gather values from users using a graphical user interface.

For example, you may use the tkinter package to create the interface below:

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()

label1 = tk.Label(root, text ='Enter your Age:')
canvas1.create_window(200, 100, window=label1)

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

def getAge ():  
    value = entry1.get()
    
    label2 = tk.Label(root, text = 'Your age is: ' + value)
    canvas1.create_window(200, 230, window=label2)
    
button1 = tk.Button(text='Get your Age', command=getAge, bg='brown', fg='white')
canvas1.create_window(200, 180, window=button1)

root.mainloop()

Once you run the code, you’ll see this display:

tkinter display

Enter an age in the entry box (for example enter the age of 27), and then click on the ‘Get your Age‘ button:

How to apply the input function in Python

The age you entered will then get printed on the display:

How to apply the input function in Python