put text on one line in python

You can use the print() function in Python to put text on one line. By default, print() adds a new line character at the end of the printed text. However, you can prevent this by passing the end parameter to the function with an empty string as its value.

Here's an example:

main.py
print("Hello", end="")
print("World!")
39 chars
3 lines

Output:

main.py
HelloWorld!
12 chars
2 lines

Alternatively, you can concatenate strings using the + operator or use string formatting to build a single string with the desired text:

main.py
text1 = "Hello"
text2 = "World!"
print(text1 + " " + text2)  # using concatenation

print("{} {}".format(text1, text2))  # using string formatting
147 chars
6 lines

Output:

main.py
Hello World!
Hello World!
26 chars
3 lines

gistlibby LogSnag