Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any simple way to add \n in r'' string in python

I need to add several paths into a single line string with a \n character at the end. For convenience, The key word r is added at the front of the string. In this case the character '\n' couldn't be display normally.

Ex.

str_output = r'name = %(name)s, some_dir = \\folder0\\..., description = "%(des)s\n'
print(str_output % {'name':'new', 'des':'new add one'})

The out put will display without line break. Currently I use string plus to by pass this problem. Such as:

str_output = r'name = %(name)s, some_dir = \\folder0\\..., description = "%(des)s' + '\n'

Instead of the previous define of str_output. I'm curious about is there any other convenience way to do this? The string plus looks ugly in my codes. Thank you!

like image 402
Erxin Avatar asked Feb 20 '13 07:02

Erxin


4 Answers

I'm a little late to the party here but this was a recent requirement for myself. I wanted a multiline axis label with LaTeX parsing for a 2-dimensional histogram in matplotlib.

My solution was to use parentheses to start line continuation, and then combine normal and raw strings to achieve the line break:

xlabel=('Time of Flight (ToF)\n'
        r'[$\times10^3$ arb. units]')

Here’s how this looks:

Here's a screen grab of the result

This can be extended to more complex strings.

like image 187
compuphys Avatar answered Nov 05 '22 16:11

compuphys


The r you're putting before the start of your string literal indicates to Python that is is a "raw" string, and that escape sequences within it should be treated as regular characters. I suspect you're doing this because of the doubled backslash characters you have, which you don't want python to interpret as a single escaped backslash character. However, the raw literal is also preventing \n from being interpreted as a newline character.

The only fixes are to keep the the raw string literal separate from the newline, then join them (like you are already doing), or to use a regular string literal and escape your backslashes:

str_output = 'name = %(name)s, some_dir = \\\\folder0\\\\..., description = "%(des)s\n'
like image 26
Blckknght Avatar answered Nov 05 '22 16:11

Blckknght


The string literal prefix r signifies a raw string, that is escape's are regular characters. If you really want to use raw strings, you can try something like:

name = 'new'
des = 'new add one'
newline = '\n'
str_output = rf'name = {name}s, some_dir = \\folder0\\..., description = "{des}s{newline}s'
print(str_output)

Although this means that you'll have to bear the newline in every dict.

Another way of doing it which has a little more meaning:

str_output = r'name = %(name)s, some_dir = \\folder0\\..., description = "%(des)s%(\n)s'
print(str_output % {'name':'new', 'des':'new add one', '\\n': '\n'})
like image 2
dmg Avatar answered Nov 05 '22 18:11

dmg


As already noted in this answer, use parentheses to combine raw strings and ordinary strings by implicit concatenation of string literals. Using also formatted string literals, which are available in Python >= 3.6, the result is:

name = 'some_name'
des = 'some decription'
message = (rf'{name = !s}s, some_dir = \folder0\..., description = {des}s' '\n')
print(message)
like image 1
Ioannis Filippidis Avatar answered Nov 05 '22 17:11

Ioannis Filippidis