newtum

Our Social Network

PYTHON INPUT FUNCTION: MAKING YOUR PROGRAMS INTERACTIVE

Python Formatted Output: Mastering print() with Style

Till now, we have seen how to use print() in Python to display messages and outputs in various formats. But there's one limitation: everything we printed was hardcoded. That means the values were constant and couldn't be changed during execution.

In real-world programming, we want our programs to be interactive — that is, accept input from the user and produce results based on it. In this blog, we will dive into the Python input function, which allows user interaction through the console.

Why We Need the Input Function

Let’s say we want to write a program to add two numbers. The basic code would be:

x = 10
y = 20
print("Sum is:", x + y)

This code works, but it uses fixed values. What if we want to enter different numbers every time? That’s where the input() function comes in.

By using input(), we can accept data from users during the program’s execution and make our code dynamic.

Using the input() Function

Let’s start with a very simple example: asking the user for their name.

name = input("Please enter your name: ")
print("Hello", name)

How This Works:

  • input() displays a message to the user and waits for input.
  • Whatever the user types is stored as a string in the variable on the left-hand side.
  • The program continues after the user presses Enter.

For Example:

Please enter your name: Newtum Hello Newtum

This small feature adds interaction and allows the program to behave differently each time.

General Syntax of input()

variable_name = input("Prompt message")

  • variable_name is where the user's input is stored
  • "Prompt message" is the text shown to the user before input
  • input() always returns a string

Let’s Try Another Example

This time, we’ll ask for a user’s favorite book and its author. Then we’ll display that information in a formatted sentence.

book_name = input("Please enter the name of your favorite book: ")
author = input("Please enter the author's name: ")
print("You love the book '%s' written by %s." % (book_name, author))

Step-by-step Explanation

  • The first input() asks for the book name and stores it in book_name
  • The second input() asks for the author's name and stores it in author
  • The print() statement uses string formatting to show a friendly output

Let’s say you run this program and enter:

Please enter the name of your favorite book: You Can Win
Please enter the author's name: Shiv Khera

You’ll get the output:

You love the book 'You Can Win' written by Shiv Khera.

Sample Code Block: Combining Input and Print

1# Accepting user details
2name = input("Please enter your name: ")
3age = input("Please enter your age: ")
4city = input("Enter your city: ")
5
6# Displaying formatted output
7print("Hello %s, you are %s years old and live in %s." % (name, age, city))

Explanation of the Code

  • "The program uses three input() statements to gather name, age, and city.
  • All inputs are stored as strings by default.
  • The final print() statement combines all values using %s placeholders for strings.
  • The % symbol followed by the tuple ((name, age, city)) inserts the values into the placeholders in the correct order.

Output

Please enter your name: Alex
Please enter your age: 25
Enter your city: Mumbai
Hello Alex, you are 25 years old and live in Mumbai.

This is a clear example of using input to create a personalized message for the user.

Bonus Topic: Converting Input into Numbers

As mentioned, input() always returns a string. If you're working with numbers, you'll need to convert them using int() or float().

x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))
print("Sum is:", x + y)
  • Here, the input is converted into integers before performing addition.
  • If you skip int(), the + operator will concatenate strings instead of adding numbers.

Example:

Enter the first number: 10
Enter the second number: 20
Sum is: 30

If you skip conversion:

x = input("Enter 1st number: ")
y = input("Enter 2nd number: ")
print("Sum is:", x + y)

The output would be:

Sum is: 1020 # Because they’re strings

Watch the Full Video on YouTube

Want to see all of this in action? Check out our complete tutorial with step-by-step explanation and demo examples. Watch the Python Input Function Tutorial on YouTube

Conclusion

With the input() function, you can make your programs interactive and user-friendly. This is an essential step toward real-world programming where user input drives the output. Start using it in your practice programs and experiment with different input formats and data types.

User input transforms a script into an experience—let your program ask, listen, and respond.

— Manoj Kolhe

More Articles

Ready to Explore the Future of Technology?

Unlock the tools and insights you need to thrive on social media with Newtum. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.

Newtum

Newtum is an emerging online academy offering a wide range of high-end technical courses for people of all ages. We offer courses based on the latest demands and future of the IT industry.

© Newtum, Inc. All Rights Reserved.