🖨️ Python Variable Output – Master the print() Function | TechTown.in

In Python, writing code is only half the job — the real magic happens when your program displays meaningful output to the user. Whether you’re showing a result, a message, or a combination of both, Python’s print() function is your best friend.

In this guide, you’ll learn how to output variables, combine text with data, and format your output cleanly — just like a pro Python developer!


🧾 Printing Variables in Python

The most basic way to display the value of a variable in Python is with the print() function.

name = "TechTown"
print(name)

Output:

TechTown

It’s that simple — just store a value in a variable and print it!


🔗 Combine Text and Variables in Output

You can use the + operator to combine (concatenate) strings:

name = "Tanmay"
print("Hello " + name)

Output:

Hello Tanmay

⚠️ Watch Out: TypeError!

You cannot directly combine strings with integers using +:

age = 25
print("Age: " + age)  # ❌ This will cause an error

✅ Fix it using str():

print("Age: " + str(age))

✅ Print Multiple Variables Using Commas

A better (and safer) way is to separate variables with commas:

name = "Tanmay"
age = 25
print("Name:", name, "| Age:", age)

Output:

Name: Tanmay | Age: 25

This way, Python automatically handles different data types for you.


✨ Advanced Output Formatting

Want cleaner, professional-style output? Try these:

1️⃣ Using f-strings (Python 3.6+)

name = "Tanmay"
age = 25
print(f"My name is {name} and I am {age} years old.")

2️⃣ Using format() method

print("My name is {} and I am {} years old.".format(name, age))

Both are perfect for combining variables into text cleanly and dynamically.


🚀 Example: Mixing Strings and Numbers

price = 49.99
item = "Headphones"
print(f"The price of {item} is ₹{price}")

Output:

The price of Headphones is ₹49.99

📘 Summary: Python Output Essentials

TaskMethod
Print string or numberprint(variable)
Concatenate string + string"Hi " + name
Concatenate string + number"Age: " + str(age)
Safe multiple outputprint("Age:", age)
Formatted output (f-string)f"My age is {age}"
Formatted output (format())"Age is {}".format(age)

🧠 Pro Tips

  • Use f-strings for clean, readable code.
  • Avoid using + to mix strings and numbers directly.
  • Use commas in print() when displaying multiple values of different types.

🏁 Conclusion

Displaying output in Python is where your code begins to interact with users. By learning how to print variables correctly and combine text with data, you make your programs more useful and user-friendly.

Keep exploring the print() function and try formatting techniques to level up your Python coding style!


🔗 Explore more practical Python tutorials at TechTown.in