1

I have a python string with newlines represented like this:

""members" : [\n\t\t{\n\t\t\t"_id ...." 

I want to store the string in a variable but with the \n and \t converted into newlines and tabs:

"members" : [
 {
 "_id" .... "

The reason is because I'm using it in a command that I'm formatting and if I use .format(string) then it formats the command with the \n instead of the actual new line

depperm
10.8k4 gold badges46 silver badges68 bronze badges
asked Feb 23, 2017 at 18:31

2 Answers 2

1

In a string literal, \t and \n normally are replaced the the tab and newline characters. To see that, however, you need to print the string as it is, not its representation. Interactive echo prints the representation.

>>> s = 'a\tb\nc'
>>> s
'a\tb\nc'
>>> print(s)
a b
c
>>> print(repr(s))
'a\tb\nc'
answered Feb 23, 2017 at 18:48
Sign up to request clarification or add additional context in comments.

Comments

0

You need to do something like that

stringVariable="""
 Text1
 text2
 text3"""

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.