Python

How to print multiple strings without line breaks in Python

March 19, 2022

How to print multiple strings without line breaks in Python

In this article we will learn how to print multiple strings without line breaks in Python, without using libraries and in a simple way

print multiple strings without line breaks cover

Hey programmers, are you ok dudes? Let’s learn more about Python and string printing!

Let’s say you have a sequence of prints, generated manually or from a loop iteration

If you use multiple prints, each one will be printed on its own line, right?

Like this example:

print("It")
print(" should ")
print(" be ")
print(" in ")
print(" one ")
print(" line.")

We have this return:

It
should
be
in
one
line.

The first solution is using end, see this example:

print("It", end="")
print("should ", end="")
print("be ", end="")
print("in ", end="")
print("one ", end="")
print("line.")

This way we will have the following output:

It should be in one line.

We also have the alternative of building a loop based on these results, but we need them to be in variables

Take a look:

a = "It"
b = "should "
c = "be "
d = "in "
e = "one "
f = "line."

prints = [a,b,c,d,e,f]

phrase = ''

for word in prints:
 phrase += word

print(phrase)

This way we will get the same result as the way we used end

These are the easiest ways to print a sequence of prints or strings in a single sentence, without breaking the line.

This way your code will become cleaner and more explicit, so that other developers make it easier to maintain.

Conclusion

In this article we learned how to print multiple strings without line breaks

We use a way to print multiple prints with the end attribute in the print method

And we also learned another strand with a for loop, where we need the strings to be in variables

Click here to learn more about Python techniques.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x