Python provides simple and flexible ways to interact with users — you can take input from the keyboard using the input() function and display output using the print() function.
This article explains how input() and print() work, along with different methods of string formatting — such as f-strings, format(), and % formatting.
The input() function allows users to enter data from the keyboard during program execution. It always returns the input as a string (text), even if you type a number.
Example:
name = input("Enter your name: ")
print("Hello,", name)
Output:
Enter your name: Julie
Hello, Julie
By default, input() returns a string. If you want numeric input, you must convert (cast) it using int() or float().
Example:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
Output:
Enter first number: 5
Enter second number: 10
Sum: 15
Output in Python — print() Function
The print() function is used to display output on the screen. It can print strings, numbers, variables, and even multiple values separated by commas.
Syntax:
print(object1, object2, ..., sep=' ', end='\n')
Example:
name = "Python"
version = 3
print("Welcome to", name, "version", version)
Output:
Welcome to Python version 3
Using 'sep' and 'end':
print("A", "B", "C", sep="-")
print("Hello", end=" ")
print("World")
When printing variables or messages, you often need to mix text with dynamic values. Python provides multiple ways to format strings neatly and clearly.
f-Strings (Python 3.6+)
f-strings (formatted string literals) are the most modern and preferred method. They allow you to embed expressions inside curly braces {} directly in a string by prefixing it with the letter f.
Example:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 25 years old.
You can even perform calculations inside {}:
Example:
a, b = 5, 10
print(f"The sum of {a} and {b} is {a + b}")
Output:
The sum of 5 and 10 is 15
The str.format() method is an older but still widely used approach. You can insert placeholders {} into a string, which are replaced by values passed to the format() function.
name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
Output:
My name is Bob and I am 30 years old.
You can also use positional and keyword arguments:
Example:
print("Hello {0}, you have {1} new messages.".format("Alice", 5))
print("Coordinates: Latitude={lat}, Longitude={lon}".format(lat=27.2, lon=78.0))
Output:
Hello Alice, you have 5 new messages.
Coordinates: Latitude=27.2, Longitude=78.0
This is the oldest way of string formatting (from C language style). It uses % as a placeholder for variable values.
Example:
name = "Charlie"
age = 22
print("My name is %s and I am %d years old." % (name, age))
Output:
My name is Charlie and I am 22 years old.
Example:
pi = 3.14159
print("Value of pi = %.2f" % pi)
Output:
Value of pi = 3.14
Summary
In this lesson, you learned how to take user input, display output, and format strings using different techniques.
In the next article, we'll cover Control Statements in Python — learning how to use if, elif, else, and nested conditions to control the flow of your program.
Comments
No comments yet. Ask a question or share what helped.