John Marshall
John Marshall

Reputation: 324

Python how to store string into a variable?

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

Upvotes: 1

Views: 9501

Answers (2)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

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'

Upvotes: 1

You need to do something like that

stringVariable="""
                  Text1
                  text2
                  text3"""

Upvotes: 0

Related Questions