Learn how to input or enter text via the input() function in python.

Example 1: Simple input() Example

This example will teach you the following:

  1. How to enter or input text via the input() function.

Step 1: Create Project

Create python project or file named main.py.

Step 2: Write Code

Add the following code:

name = input()
print('Hello,', name)

Step 3: Run

Run the code by navigating to the project directory:

cd your_project_folder

Then execute:

python main.py

You will prompted to enter name in the command promt, so for example I type Jairo

You will get the following:

Jairo
Hello, Jairo

Example 2: Simple Addition calculator

In this example you will learn:

  1. How to use input() function to create a simple calculator.

Step 1: Create Project

Create python project or file named main.py.

Step 2: Write Code

Add the following code:

def sum(a, b):
    return (a + b)

a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))

print(f'Sum of {a} and {b} is {sum(a, b)}')

Step 3: Run

Run the code by navigating to the project directory:

cd your_project_folder

Then execute:

python main.py

You will get the following:

Enter 1st number:
65
Enter 2nd number:
35
Sum of 65 and 35 is 100

Categorized in: