Input in python

Understanding the input() Function in Python

The input() function in Python is used to take user input.
When this function is called, the program pauses and waits for the user to type something and press Enter.


Basic Example of input()

Here’s a simple example:

name = input("Enter your name: ") print("Welcome " + name)

How it works:

  • The program displays: Enter your name:

  • The user types their name and presses Enter.

  • The entered text is stored in the variable name.

  • The program then prints a welcome message with the user’s name.


Example with Multiple Inputs

You can use input() multiple times to collect different pieces of information:

name = input("Enter your name: ") age = input("Enter your age: ") print("Welcome " + name) print("Your Age is " + age)

What happens here:

  • The program first asks for your name, then for your age.

  • Both inputs are stored in variables (name and age).

  • The program prints both values.


Taking Integer Inputs and Performing Addition

By default, input() always returns a string.
If you want to do math with numbers entered by the user, you need to convert the string to an integer using int().

Example:

# Python Program to add two numbers from user input x = input("Enter First Number: ") y = input("Enter Second Number: ") x = int(x) y = int(y) res = x + y print("Sum is", res)

How it works:

  • The program asks for two numbers.

  • The inputs are initially stored as strings (x and y).

  • int(x) and int(y) convert the inputs to integers.

  • The sum is calculated and printed.


Shortcut: Convert Input to Integer Immediately

You can simplify your code by converting the input directly in the input() call:

# Python Program to add two numbers (short version) x = int(input("Enter First Number: ")) y = int(input("Enter Second Number: ")) res = x + y print("Sum is", res)

๐Ÿ‘€This approach is cleaner and easier to read!


Using input() in Online Coding Environments

When you run a program that uses input(), you’ll see prompts like this:

Enter First Number: 10 Enter Second Number: 20

The program will output:

Sum is 30

Special Note: Competitive Programming

In competitive programming, it’s often better to avoid adding text prompts inside input().
Why? Because online judges expect only the result, not extra text.

Example without prompts:

# Python Program to add two numbers (for competitive programming) x = int(input()) y = int(input()) res = x + y print(res)

If you enter:

10 20

The program will simply output:

30

๐Ÿ‘‰ This avoids unwanted extra text and ensures your output matches exactly what is expected.


Key Points to Remember

✅ The input() function always returns a string. Use int() (or float()) to convert when needed.
✅ In competitive programming, avoid using prompts like "Enter a number:" in your input() calls.
✅ Make sure your program prints only the required output, especially when an exact format is expected.


๐Ÿ‘Now you know how to use the input() function to interact with your users!
This is one of the simplest ways to make your Python programs more dynamic and fun.

Popular Posts