Simple Output Formatting | Python Tutorial - Learn with VOKS
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Back

Simple Output Formatting

Learn this topic step-by-step with VOKS Tutorials.

Output formatting means displaying information in a neat, organized, and readable way.

For example:

Instead of this messy output:

Danny 25 95.5

We want something cleaner like:

Name: Danny
Age: 25
Score: 95.5

๐Ÿ“Œ 1. Basic Output Using print()


print("Hello World")

You can also print variables:


name = "Danny"
print(name)

๐Ÿ“Œ 2. Printing Multiple Values


name = "Danny"
age = 25

print(name, age)

By default, Python separates them with a space.


๐Ÿ“Œ 3. Using sep (Separator)

The sep parameter controls how items are separated.


print("Python", "Java", "C++", sep=" | ")

Output:


Python | Java | C++

๐Ÿ“Œ 4. Using end (Line Ending)

By default, print() ends with a new line (\n).

You can change it:


print("Hello", end=" ")
print("World")

Output:


Hello World

๐Ÿ“Œ 5. f-Strings (Best & Modern Method โœ…)

This is the easiest and most recommended method.


name = "Danny"
age = 25

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

Why f-strings?

  • Easy to read
  • Fast
  • Clean
  • Modern (Python 3.6+)

๐Ÿ“Œ 6. Controlling Decimal Places


price = 19.9876
print(f"Price: {price:.2f}")

Output:


Price: 19.99

.2f means:

  • 2 decimal places
  • f = float

๐Ÿ“Œ 7. Setting Width (Alignment)

You can control spacing.


name = "Danny"
print(f"{name:10}")

This means:

  • Reserve 10 spaces

Alignment Options


| Format | Meaning | Example |
|--------|----------|----------|
| :<10 | Left align | f"{name:<10}" |
| :>10 | Right align | f"{name:>10}" |
| :^10 | Center align | f"{name:^10}" |

Example:


print(f"{'Python':<10}")
print(f"{'Python':>10}")
print(f"{'Python':^10}")

๐Ÿ“Œ 8. Formatting Numbers

Add Commas (for thousands)


number = 1000000
print(f"{number:,}")

Output:


1,000,000

Percentage Format


score = 0.85
print(f"{score:.0%}")

Output:


85%

๐Ÿ“Œ 9. Using format() Method

Older but still useful.


name = "Danny"
age = 25

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

You can also number them:


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

๐Ÿ“Œ 10. Old % Formatting (Very Old Method)


name = "Danny"
age = 25

print("My name is %s and I am %d" % (name, age))

๐Ÿ“Š Comparison of Formatting Methods


| Method | Easy to Read | Modern | Recommended |
|--------|--------------|----------|--------------|
| f-string | Yes | Yes | โœ… Best |
| format() | Medium | Yes | โœ” Good |
| % operator | Harder | No | โŒ Old |

๐Ÿ“Œ 11. Escape Characters in Output


| Escape | Meaning |
|--------|----------|
| \n | New line |
| \t | Tab space |
| \\ | Backslash |
| \" | Double quote |
| \' | Single quote |

Example:


print("Name:\tDanny")

Output:


Name:   Danny

๐Ÿ“Œ 12. Printing a Simple Table

You can format output like a table:


name = "Danny"
age = 25
score = 92.456

print(f"{'Name':<10}{'Age':<5}{'Score':<10}")
print(f"{name:<10}{age:<5}{score:<10.2f}")

Output:


Name      Age  Score
Danny     25   92.46

๐Ÿ“Œ 13. Real-Life Example (Student Report)


name = "Danny"
math = 85
english = 90

average = (math + english) / 2

print("----- REPORT -----")
print(f"Name: {name}")
print(f"Math: {math}")
print(f"English: {english}")
print(f"Average: {average:.2f}")


QUICK KNOWLEDGE CHECK

Test Your Understanding

Answer 5 questions generated from this lesson. Your result is calculated instantly and is not saved.

0 / 5 answered
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Course contents
Python
01 Intorduction 02 Python Syntax Compare to other Programming Languages 03 How to Install Python 04 Print Statement 05 Python Comments 06 Data Structure and Data Type 07 String Operations in Python 08 Simple Input and Output 09 Simple Output Formatting