0

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')?

swiss_knight
  • 5,787
  • 8
  • 50
  • 92
  • Do you mean suppress the newline that `print()` prints after every call? If so, you can use the `end` keyword to do so: `print(, end='')`. – Christian Dean Jun 29 '17 at 22:30
  • Also please consider using `print("Hello world, {} {} {}".format(1,2,3))` – Torxed Jun 29 '17 at 22:30
  • Duplicate: https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string – Maya Jun 29 '17 at 22:36
  • 2
    Possible duplicate of [Pythonic way to create a long multi-line string](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) – Maya Jun 29 '17 at 22:43

3 Answers3

2

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))
syntaxError
  • 896
  • 8
  • 15
  • This will also print the indent tabs out resulting in an ugly result. One need to separate thins on new lines as the example Alan Leuthard has explained. – swiss_knight Jul 01 '17 at 19:00
  • 1
    There is a left align in the string module, you can also just leftstrip it. – syntaxError Jul 01 '17 at 19:40
1

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.

Alan Leuthard
  • 798
  • 5
  • 11
  • This seems better and cleaner, thanks. But with the last solution, I got ugly large blanks when the code is indented as it took the indent tabs into account. So, first variant is better. Without the "plus" signs. – swiss_knight Jul 01 '17 at 19:01
0

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)) 
                                     ) 
                     )
swiss_knight
  • 5,787
  • 8
  • 50
  • 92