Python print() End Parameter: How to Change Line Endings
Use Python’s print(end=...) to control what follows printed text: keep the default newline, add a space, or join output on the same line.
Pass a string as the end keyword argument to choose what print() writes after its objects. The default is a newline ('\n'); use end=' ' to continue on the same line with a space, or end='' to add no ending at all.
print('Hello', end='!') # writes Hello!
print('Hello', end='') # writes Hello without a newline
The complete signature is print(*objects, sep=' ', end='\n', file=None, flush=False). The built-in reference documents the parameters and their behavior in the Python built-in functions reference. The Python input and output tutorial also demonstrates using end=' ' to keep loop output on one line.
1. Choose the ending you need
| Goal | Code | Effect |
|---|---|---|
| Start the next output on a new line | print('Hello') |
Writes Hello, then the default newline. |
| Continue on the same line with a space | print('Hello', end=' ') |
Writes Hello ; the next write follows the space. |
| Continue immediately with no separator | print('Hello', end='') |
Writes Hello and leaves the cursor on that line. |
| Append custom punctuation or a marker | print('Loading', end='...') |
Writes Loading.... |
end is the suffix for one call to print(). It does not change how multiple objects in that call are separated; use sep for that.
print('red', 'green', 'blue', sep=', ', end='!\n')
# red, green, blue!
2. Runnable examples
Join several values on one line
for number in range(1, 6):
print(number, end=' ')
print() # finish the row with the default newline
This writes 1 2 3 4 5 and then ends the line. If the trailing space is undesirable, build a string and print it once:
numbers = range(1, 6)
print(*numbers, sep=' ')
# 1 2 3 4 5
Write consecutive pieces without a newline
print('No newline', end='')
print(' — continued on the same line')
# No newline — continued on the same line
The second call adds its own default newline after its text. Until then, the output from both calls occupies the same line.
Print a progress marker
for step in range(1, 4):
print(f'Step {step}', end='...')
print('done')
Use a newline when the next message should begin on a fresh line. For an updating progress display that rewrites the current line, use a carriage return explicitly and flush the output when needed:
import sys
import time
for percent in (0, 25, 50, 75, 100):
print(f'Progress: {percent}%', end='\r', flush=True)
time.sleep(0.1)
print() # move to a fresh line after the update loop
A carriage return (\r) moves the cursor to the start of the current line in many terminals; it is different from a newline (\n). Terminal behavior and captured output can vary, so use a newline-separated log when each progress update must remain visible in files or CI logs.
3. How end works with the other parameters
sep separates objects; end ends the call
For print(a, b), sep goes between a and b; end is written once after b. The defaults are a single space for sep and a newline for end.
print('A', 'B', sep=' | ', end=' <done>\n')
# A | B <done>
Both values must be strings or None. Supplying None selects the respective default. When provided, sep, end, file, and flush must be passed by keyword.
No objects
With no objects, print() writes just its ending. That makes print() a convenient blank line, while print(end='---') writes only the marker.
Output destination and flushing
By default, output goes to sys.stdout. You can direct it to a text file object that provides write(string). The flush argument controls whether the stream is forcibly flushed after the call.
with open('output.txt', 'w', encoding='utf-8') as output:
print('first', end=' ', file=output)
print('second', file=output)
For interactive progress output, flush=True can make each update visible promptly, especially when the destination is buffered. Flushing every write can add overhead; leave the default for ordinary output unless prompt visibility matters.
4. Line endings in terminals and files
end chooses the string Python passes to the output stream after the printed objects. In the usual console case, end='' suppresses the newline that print() would otherwise add.
Text file objects may translate newline characters according to their newline settings and the platform. That translation is separate from print()‘s end parameter. If a file’s exact bytes matter, account for text versus binary output and the stream’s newline configuration; binary file objects do not accept text through print(), so use the binary file’s write() method instead.
5. Common errors and fixes
| Problem | Cause | Fix |
|---|---|---|
SyntaxError after writing print('x', end=' ', 'y') |
Keyword arguments such as end must follow positional arguments. |
Put the keyword argument last: print('x', 'y', end=' '). |
TypeError because end is an integer or another non-string |
end accepts a string or None. |
Convert the value first, for example end=str(suffix). |
| Calls still appear on separate lines | The call that follows may still use its default newline, or the stream/terminal may render output differently. | Set end on each call whose ending you want to control; inspect the actual output stream if it is redirected. |
| Progress updates do not appear immediately | The output may be buffered. | Use flush=True for updates that need to appear promptly. |
| There is an unwanted space at the end of a row | Each loop iteration printed a value with end=' '. |
Print a joined string or use argument unpacking with sep, such as print(*values, sep=' '). |
| Writing to a binary file fails | print() writes text to a text stream. |
Encode the data and write bytes with the binary file object’s write() method. |
6. Performance and reliability notes
- For a small number of values, choosing
endis a layout decision; it does not require another library. - For large output, avoid relying on thousands of tiny writes when a single joined string is clearer and more efficient for your use case.
- Use
flush=Truewhen delayed output would confuse a person or automation. Otherwise, the default buffering behavior is usually appropriate. - When output is consumed by scripts, tests, or log processors, prefer stable separators and explicit newlines. Carriage-return progress displays are intended for interactive terminals.
- When writing files, configure encoding and newline handling intentionally if output must be portable or byte-for-byte consistent.
7. Or skip the browser setup
If your workflow also needs website screenshots, ScreenshotNeo is a website screenshot API and MCP server. One GET request returns a PNG, JPEG, WebP, or PDF. See the API documentation for options and setup.
curl -G "https://api.screenshotneo.com/v1/shot" \
-d access_key=YOUR_API_KEY \
--data-urlencode url=https://stripe.com \
-o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
const bytes = new Uint8Array(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', bytes));
- Cookie banners are accepted and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before the shot; each step can be turned off.
- Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing. Response headers report the page verdict and billing status.
- An MCP server gives AI agents tools for screenshots, page information, and PDF capture.
- The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots.
Sign up free for 1,000 screenshots a month, no card required.
8. FAQ
Can I use end with an f-string?
Yes. The f-string supplies the object text; end is still a separate keyword argument, for example print(f'Count: {count}', end=' ').
Does end='' remove a newline already inside my string?
No. It only controls the string appended after the printed objects. Newline characters already present in an object are written as part of that object.
Can end contain more than one character?
Yes. It can be any string, including multiple characters or newline characters. Choose the exact suffix your output format needs.


