Hintor
Python Basics for Programmers

Writing Clean Python Output

Python for Developers/Python Basics for Programmers/

Writing Clean Python Output

Python

Lesson explanation

Writing Clean Python Output


🎯 Learning Objective

After this lesson, you will be able to:

  • Print fixed text and multiple values clearly.
  • Use str() when string concatenation requires text conversion.
  • Prefer f-strings for readable variable-driven output.
  • Control separators and line endings with sep and end.
  • Create line breaks with \n and simple separators with string repetition.
  • Format decimal values with .2f.

Short Explanation

Python uses print() to send values to the console. A single call can print text, numbers, or several values at once:

Python
print("Retries:", 3)

When print() receives several values, it converts them for display and places a space between them by default. This means you usually do not need str() just to print a number.

You do need str() when you combine text and a number with +, because string concatenation requires both sides to be strings:

Python
"Port: " + str(8080)

For labeled output, f-strings are usually clearer:

Python
port = 8080
print(f"Port: {port}")

sep changes what appears between multiple printed values, while end changes what appears after a print() call. The default separator is one space, and the default ending is a newline.

Use \n when a single string needs an explicit line break. Repeating a short string with * is useful for simple console separators. For decimal display, a format such as .2f shows exactly two digits after the decimal point.

💡 Tip: str() belongs here because it can be necessary while building output. int() and float() convert values into numbers, so they will be covered with variables and type conversion rather than inside this output lesson.


Clean Code Examples

1. Print text and a value

Pass multiple values to print() when you want Python to display them with a space between them.

Python
retry_limit = 4
print("Retry limit:", retry_limit)

Output:

TEXT
Retry limit: 4

Python displays the integer directly. No explicit conversion is needed.

2. Convert a number when concatenating strings

The + operator can only concatenate strings with strings, so the number must be converted first.

Python
port = 8080
message = "Port: " + str(port)
print(message)

Output:

TEXT
Port: 8080

An f-string is usually cleaner for this kind of output, but understanding str() helps you recognize and fix type errors.

3. Use an f-string for labeled output

Place a variable inside {} to include its current value in the text.

Python
environment = "staging"
print(f"Environment: {environment}")

Output:

TEXT
Environment: staging

The f before the opening quote tells Python to evaluate values inside the braces.

4. Control the separator with sep

sep replaces the default space between values passed to the same print() call.

Python
print("frontend", "api", "worker", sep=" -> ")

Output:

TEXT
frontend -> api -> worker

The separator is inserted only between the values.

5. Continue on the same line with end

By default, print() ends with a newline. Change end when the next call should continue on the same line.

Python
print("Upload", end="... ")
print("complete")

Output:

TEXT
Upload... complete

The second print() starts immediately after the custom ending from the first call.

6. Create line breaks and repeat text

Use \n for an explicit line break inside one string. Use * to repeat a short string.

Python
print("-" * 12)
print("Build passed\nArtifacts ready")

Output:

TEXT
------------
Build passed
Artifacts ready

The first line repeats - twelve times. The \n splits the second string across two lines.

7. Format a decimal value

Use .2f inside an f-string to display a number with two digits after the decimal point.

Python
response_time = 18.456
print(f"Average response: {response_time:.2f} ms")

Output:

TEXT
Average response: 18.46 ms

Formatting changes how the value is displayed; it does not change the original variable.



⚠️ Common Mistakes

  1. Do not concatenate a string and a number with + unless the number is converted with str().
  2. Do not confuse sep with end: sep goes between values, while end comes after the entire call.
  3. Do not overlook spaces, punctuation, or line breaks when the required output must match exactly.
  4. Do not assume .2f changes the stored number; it only controls the displayed format.
  5. Do not hardcode calculated or provided values into the final text when the task requires using existing variables.
Start Practice

Python for Developers

Writing Clean Python Output

4 tasks
Lesson progress0 / 4 tasks completed

Tasks

Practice progress

Complete a port label

View task

Complete the missing conversion so the string concatenation succeeds.

Requirements

  • Keep the existing variable and text unchanged.
  • Fill only the missing function name.
  • Print exactly the expected output.

Expected Output

TEXT
Port: 8080

Expected output

Port: 8080
Code: PythonTask 1

Complete a port label

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 4 tasks completed

Back to unitBack to track
GoalRecommended Python
Print several valuesprint(label, value)
Concatenate text with a number"Count: " + str(count)
Build readable labeled outputf"Count: {count}"
Change the separator`print(a, b, sep="
Keep the next output on the same lineprint(text, end="...")
Show two decimal placesf"{amount:.2f}"