gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
python how to not print newline
- User
python how to not print newline
- Assistant
In Python, by default, the
print()function adds a newline character at the end of the output. If you want to print without adding a newline, you can use theendparameter of theprint()function.Here's how to do it:
# Default behavior (adds a newline) print("Hello") print("World") # Print without a newline print("Hello", end=' ') print("World", end=' ')In this example, the first two
print()statements will print "Hello" and "World" on separate lines. The second pair ofprint()statements usesend=' 'to specify a space instead of the default newline, so it prints "Hello World" on the same line.You can set
endto any string you want. For example, if you'd like to separate outputs with a comma:print("Hello", end=', ') print("World")This will output:
Hello, World(with no newline between the two prints).