Home > Blockchain >  How to save " \\n " when removing " \n " in string
How to save " \\n " when removing " \n " in string

Time:04-29

>>> b = 'first \\n second \n third \\n forth \n fifth \\n'
>>> print(b)
first \n second 
 third \n forth 
 fifth \n

b to wanted result,
print( result )
first \n second third \n forth fifth \n
result = 'first \n second third \n forth fifth \n'
how to do this?

CodePudding user response:

\n is an escape character, which indicates a line break, while \\n is a backslash and a letter n, which will not be affected when you delete \n:

>>> b = 'first \\n second \n third \\n forth \n fifth \\n'
>>> b.replace('\n', '')
'first \\n second  third \\n forth  fifth \\n'

This example may make you realize the difference between them:

>>> len('\n')
1
>>> list('\n')
['\n']
>>> len('\\n')
2
>>> list('\\n')
['\\', 'n']

CodePudding user response:

Maybe you can try b.replace('\n', '')

  • Related