Maxim Lott
Maxim Lott

Reputation: 360

Create new line in a python string that does NOT effect output?

is it possible to create a new line in a string in one's python code that does NOT effect output?

Like:

print "This
is
my
string"

But the output when the program is run would just be "This is my string"

Just for purposes of formatting the program in a more readable way for very long strings.

Upvotes: 0

Views: 1119

Answers (3)

Hammad Manzoor
Hammad Manzoor

Reputation: 154

You could also add a comma after every print statement to print the next output on the same line :

print "This",
print "is",
print "my",
print "string" 

Upvotes: 2

cco
cco

Reputation: 6281

You could use implicit string joining too:

print("this "
      "is "
      "my "
      "string")

Upvotes: 5

jonrsharpe
jonrsharpe

Reputation: 122032

You can invoke explicit line joining, adding a backslash at the end of each line:

>>> print "this \
... is \
... my \
... string"
this is my string

Upvotes: 2

Related Questions