Basic Input and Output

Interacting with users is one of the most important parts of programming. Every useful program needs a way to **receive input** and **display output** — to gather information and communicate results. In Python, this is handled elegantly through the built‑in `input()` and `print()` functions.

Chapter 1: Introduction to Python Programming

Sub-chapter: Basic Input and Output

Interacting with users is one of the most important parts of programming. Every useful program needs a way to receive input and display output — to gather information and communicate results. In Python, this is handled elegantly through the built‑in input() and print() functions.


🧠 Understanding Input and Output

In this section, we’ll explore both concepts in depth and learn how to format data for beautiful, human‑readable output.


🧩 Basic Input in Python

Python provides the built‑in input() function to receive user input as a string (text). You can display a prompt to guide the user.

# Example: Basic input
name = input("Enter your name: ")
print("Hello, " + name + "!")

💡 How it works:

  1. Python displays the message inside the parentheses.
  2. The program pauses, waiting for the user to type a response.
  3. The input value (as text) is stored in the variable name.
  4. The greeting message is printed back to the user.

🧮 Converting Input to Numbers

All data from input() is treated as text (str).
If you want to work with numbers, you must convert the input manually:

age = input("Enter your age: ")
age = int(age)  # convert string to integer
print(f"Next year, you’ll be {age + 1} years old!")

⚠️ Be cautious — entering something that isn’t a number will cause an error (ValueError).

To prevent this, you can use exception handling:

try:
    age = int(input("Enter your age: "))
    print(f"Next year, you’ll be {age + 1}!")
except ValueError:
    print("Please enter a valid number!")

💬 Basic Output in Python

Output lets your program display text, numbers, and results. The print() function is Python’s primary tool for output.

# Example: Basic output
age = 30
print("Your age is:", age)

Here, print() automatically converts non‑string values (like numbers) into strings for display.
You can print multiple values separated by commas — Python automatically adds spaces.

print("Temperature:", 25, "°C")

Output:

Temperature: 25 °C

🧾 String Formatting Techniques

Readable output is key to professional programs. Python provides multiple methods to format your text neatly.

name = "Alice"
age = 25
print(f"Name: {name}, Age: {age}")

f‑strings are the most modern and efficient way to embed variables directly inside text.

You can even perform expressions inline:

x = 5
y = 3
print(f"The sum of {x} + {y} is {x + y}")

🧷 2. The format() Method

A slightly older but still common formatting style:

name = "Bob"
age = 30
print("Name: {}, Age: {}".format(name, age))

You can also specify placeholder positions:

print("The coordinates are ({1}, {0})".format(10, 20))

🧷 3. Percent (%) Formatting (Legacy Style)

name = "Charlie"
age = 22
print("Name: %s, Age: %d" % (name, age))

While still supported, this method is less preferred in modern Python.


🔡 Escape Sequences in Strings

Escape sequences are special characters preceded by a backslash () used to represent actions or symbols that are otherwise difficult to type.

EscapeMeaningExampleOutput
\nNewline"Hello\nWorld"Hello
World
\tTab"A\tB\tC"A    B    C
\\Backslash"C:\\Users\\Alice"C:\Users\Alice
\"Double quote"She said, \"Hi!\""She said, “Hi!”
\'Single quote'It\'s OK'It’s OK

Example:

print("Line 1\nLine 2\nLine 3")

🧑‍💻 Providing User-Friendly Input Prompts

Good user prompts make your program easier to use and less confusing.

age = input("Please enter your age: ")
print(f"You entered: {age}")

You can even combine prompts with default examples:

city = input("Enter your city (e.g., Paris): ")
print(f"You live in {city}.")

🧠 Combining Input and Output

Let’s combine what we’ve learned into a simple interactive script:

# Simple interactive script
name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello, {name}! You’ll turn {age + 1} next year.")

Output:

What is your name? Alice
How old are you? 25
Hello, Alice! You’ll turn 26 next year.

🧰 Multiple Line Output and Custom Separators

You can control how print statements behave using sep (separator) and end parameters.

print("apple", "banana", "cherry", sep=", ")
print("End of line", end=" 🚀\n")

Output:

apple, banana, cherry
End of line 🚀

🧾 Key Takeaways


You now know how to interact with users through Python’s input and output system — the first step toward building interactive and user-driven applications.