-1

how do I print these statements in next line? I tried \n but the output is displaying \n instead of printing the statements in next line. I wish to use formatter. The code is :

formatter = "%r %r %r %r"

print formatter  %(
"I had this thing.\n",
    "That you could type up right.\n ",
    "But it didn't sing.\n",
     "So I said goodnight."
    )
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
Richa
  • 151
  • 1
  • 3
  • 8

1 Answers1

1

It's working with %s instead or %r :

formatter = "%s %s %s %s"

print formatter  %(
"I had this thing.\n",
    "That you could type up right.\n ",
    "But it didn't sing.\n",
     "So I said goodnight."
    )

%r uses the repr method, without formating the special characters :

print(repr('\ntext'))
>>> '\ntext'

print(str('\ntext'))
>>>
text

If you need to keep the raw strings for some lines, you should change the formater to this pattern, and use r"rawstrings with special characters"+'\n' to add a newline when you need it.

formatter = "{}{}{}{}"

print(formatter.format(
    r"C:\n"+'\n',
    "That you could type up right.\n",
    "But it didn't sing.",
     "So I said goodnight."
    ))

# >>>C:\n
# That you could type up right.
# But it didn't sing.So I said goodnight.    

print(formatter.format(
    1,2,3,4)
    )        
# >>> 1234
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • thanks , thats because I think you cannot use /n while printing raw data (%r) but what is the alternative to printing raw data on next line? – Richa Aug 31 '17 at 19:01
  • maybe using another formater : `formatter = "%r\n %r\n %r\n %r"` ? – PRMoureu Aug 31 '17 at 19:03
  • but this will print all the formatters in the next line. For e.g. : formatter = "%r %r %r %r" print formatter % (1,2,3,4) print formatter %( "I had this thing.\n", "That you could type up right.\n ", "But it didn't sing.\n", "So I said goodnight." ) The below string statements need to be printing on next lines – Richa Aug 31 '17 at 19:11