A useful program takes data from the user and shows results. Python does this with the input() and print() functions.
Reading Input
The function input() reads a line typed by the user and always returns it as a string. A prompt message can be given inside the brackets. To use the value as a number, it must be converted, for example age = int(input("Age: ")). Several values can be read on one line with split(), for example a, b = map(int, input().split()).
Definition: input(). A built-in function that displays an optional prompt, waits for the user to type a line and returns it as a string.
Printing Output
The function print() displays values on the screen. Several values can be given separated by commas, and they are shown with a space between them. The argument sep changes the separator and end changes what is printed at the end, which is a newline by default. For example, print("a", "b", sep="-", end="!") prints a-b!.
Formatting Output
Output can be formatted with f-strings, which put variables inside curly brackets in a string that starts with f. A format code after a colon controls the display, for example {x:.2f} gives two decimal places and {n:5d} reserves a width of five. The older ways are the % operator and the format() method.
A program often needs to repeat reading until the user types a valid value, but even a simple program should be written so that the prompt tells the user what is expected. A prompt such as Enter marks out of 100 is far clearer than an empty prompt. When several results are printed, using f-strings keeps the code short and the layout tidy. The older print statement of Python 2 had no brackets, and programs written for it must be changed before they run on Python 3.
Table 1.4: Common format codes
| Code | Meaning | Example result |
|---|---|---|
| .2f | Two decimals | 3.14 |
| 5d | Width 5 integer | " 25" |
| < or > | Left or right align | Alignment |
| , | Thousands separator | 1,000 |
Example 1.4: Reading numbers and printing a sum
Problem: Read two integers a and b and print their sum and average with two decimals.
Solution:
- Read each value with input() and convert with int().
- Compute the sum and the average.
- Print with an f-string and the format code .2f.
Result: For 7 and 4 the program prints Sum = 11 and Average = 5.50.
a = int(input("a: "))
b = int(input("b: "))
print(f"Sum = {a + b}")
print(f"Average = {(a + b) / 2:.2f}")
print("x", "y", "z", sep="-", end="!\n")
Input
7
4
Output
a: b: Sum = 11
Average = 5.50
x-y-z!
Common mistakes
- Using the result of input() as a number without converting it.
- Forgetting the f before the string, so the braces are printed as they are.
- Expecting print() to stay on the same line without setting end.
Key takeaways
- input() always returns a string.
- print() uses sep and end to control spacing.
- f-strings use {value:format}.
- Convert with int() or float() before calculating.
Practice questions
- What type does input() return?
- What does the argument end control in print()?
- Write the format code for two decimal places.
Answers to the practice questions
- A string.
- What is printed at the end, which is a newline by default.
- .2f.
Sources
- Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit I
