swiss_knight
swiss_knight

Reputation: 7921

print \n but not aesthetic line breaks?

How not to print the line break in code? (Python 2.7)

Example:

print("Hello world, this is a very very long sentence with variables 1:\n%s\nVariable 2:\n%s\nVariable 3:\n%s\nVariable 4:\n%s\nVariable 5:\n%s" % (var1, var2, var3, var4, var5) )  

In fact there is more variables but I cut this to 5 for the example.

I do need the '\n'.

For a better readability, I need to write this print statement on several lines within the code. But inserting some line breaks makes these line breaks appear on the output (which I do not want to).

Is there a way not to display line breaks (but keeping display the desired '\n')?

Upvotes: 0

Views: 246

Answers (3)

swiss_knight
swiss_knight

Reputation: 7921

The solution that worked for me without the indent tabs looks so:

                printtxt = ("Variables {} <- {} estimation parameters are:\n"
                            "Retval: {}\n"
                            "Charge transfert:\n {} \n"
                            "Amount:\n {} \n"
                            "Stats:\n {} \n"
                            "ZHA:\n {} \n"
                            "First eig (rad):\n {} \n"
                            "First eig (deg.):\n {} \n"
                            "Trans. :\n {} \n"
                            "Valids:\n {} \n"
                            "Dot product:\n {} \n"
                            )

                print(printtxt.format(var1, var2, var3, 
                                      var4, var5, var6, 
                                      var7, var8,
                                      (var90,var91,var92), 
                                      var10, var11,
                                      (np.dot(A,B.T)) 
                                     ) 
                     )

This also works for writing the results in a file:

                f1.write(printtxt.format(var1, var2, var3, 
                                      var4, var5, var6, 
                                      var7, var8,
                                      (var90,var91,var92), 
                                      var10, var11,
                                      (np.dot(A,B.T)) 
                                     ) 
                     )

Upvotes: 0

Alan Leuthard
Alan Leuthard

Reputation: 798

There are several ways to break up a print statement. From your description (keeping explicit line breaks) you want:

print(
    "Hello world, this is a very very long sentence with variables " +
    "1:\n" +
    "{}\n".format(var1) +
    "Variable 2:\n" +
    "{}\n".format(var2) +
    "Variable 3:\n" +
    "{}\n".format(var3) +
    ...etc
)

which results in:

>>> var1 = "test1"
>>> var2 = "test2"
>>> var3 = "test3"
>>> print(
        "Hello world, this is a very very long sentence with variables " +
        "1:\n" +
        "{}\n".format(var1) +
        "Variable 2:\n" +
        "{}\n".format(var2) +
        "Variable 3:\n" +
        "{}\n".format(var3)
    )
Hello world, this is a very very long sentence with variables 1:
test1
Variable 2:
test2
Variable 3:
test3

>>> 

I much prefer:

print(
"""
Hello world, this is a very very long sentence with variables 1:
{}
Variable 2:
{}
Variable 3:
{}
""".format(var1, var2, var3)
)

...for the same results.

Upvotes: 1

syntaxError
syntaxError

Reputation: 866

I would personally just use a multi-line string as the template, makes the code much cleaner. If you want more features to play around with look at the string module.

variables = (1, 2, 3, 4, 5)


text ="""
Hello world, this is a very ver long sentence with variables:
Varible 1:
{}
Varible 2
{}
Varible 3:
{}
Varible 4:
{}
Varible 5:
{}"""

print(text.format(*variables))

Upvotes: 2

Related Questions