Wednesday, 15 April 2015

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



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

i need add together several paths single line string \n character @ end. convenience, key word r added @ front end of string. in case character '\n' couldn't display normally.

ex.

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

the out set display without line break. utilize string plus pass problem. such as:

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

instead of previous define of str_output. i'm curious there other convenience way this? string plus looks ugly in codes. give thanks you!

the r you're putting before start of string literal indicates python is "raw" string, , escape sequences within should treated regular characters. suspect you're doing because of doubled backslash characters have, don't want python interpret single escaped backslash character. however, raw literal preventing \n beingness interpreted newline character.

the fixes maintain the raw string literal separate newline, bring together them (like doing), or utilize regular string literal , escape backslashes:

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

python string format

No comments:

Post a Comment