Lesson explanation
After this lesson, you will be able to:
str() when string concatenation requires text conversion.sep and end.\n and simple separators with string repetition..2f.Python uses print() to send values to the console. A single call can print text, numbers, or several values at once:
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:
"Port: " + str(8080)For labeled output, f-strings are usually clearer:
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()andfloat()convert values into numbers, so they will be covered with variables and type conversion rather than inside this output lesson.
Pass multiple values to print() when you want Python to display them with a space between them.
retry_limit = 4
print("Retry limit:", retry_limit)Output:
Retry limit: 4Python displays the integer directly. No explicit conversion is needed.
The + operator can only concatenate strings with strings, so the number must be converted first.
port = 8080
message = "Port: " + str(port)
print(message)Output:
Port: 8080An f-string is usually cleaner for this kind of output, but understanding str() helps you recognize and fix type errors.
Place a variable inside {} to include its current value in the text.
environment = "staging"
print(f"Environment: {environment}")Output:
Environment: stagingThe f before the opening quote tells Python to evaluate values inside the braces.
sepsep replaces the default space between values passed to the same print() call.
print("frontend", "api", "worker", sep=" -> ")Output:
frontend -> api -> workerThe separator is inserted only between the values.
endBy default, print() ends with a newline. Change end when the next call should continue on the same line.
print("Upload", end="... ")
print("complete")Output:
Upload... completeThe second print() starts immediately after the custom ending from the first call.
Use \n for an explicit line break inside one string. Use * to repeat a short string.
print("-" * 12)
print("Build passed\nArtifacts ready")Output:
------------
Build passed
Artifacts readyThe first line repeats - twelve times. The \n splits the second string across two lines.
Use .2f inside an f-string to display a number with two digits after the decimal point.
response_time = 18.456
print(f"Average response: {response_time:.2f} ms")Output:
Average response: 18.46 msFormatting changes how the value is displayed; it does not change the original variable.
+ unless the number is converted with str().sep with end: sep goes between values, while end comes after the entire call..2f changes the stored number; it only controls the displayed format.Python for Developers
Assembled code
Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.
0 / 4 tasks completed
| Goal | Recommended Python |
|---|
| Print several values | print(label, value) |
| Concatenate text with a number | "Count: " + str(count) |
| Build readable labeled output | f"Count: {count}" |
| Change the separator | `print(a, b, sep=" |
| Keep the next output on the same line | print(text, end="...") |
| Show two decimal places | f"{amount:.2f}" |