user11727742
user11727742

Reputation:

How to print without new line using pprint

How to pprint.pformat string as like print. I am using pprint because I need to use indent

test = 'google\\nfirefox'
import pprint
pprint.pformat(test)

output is "'enable\\\\nshow'"

expected result is like

print (test)
google\nfirefox

Upvotes: 2

Views: 1837

Answers (3)

Nuno Pissarra
Nuno Pissarra

Reputation: 96

Hei,

If you want to print in lines splitet by '\n', you can try :

print(test, end ='\n')

If you want to print all text in the same line, you can try to replace '\n' by a space:

test. replace("\n", " ")
print(test)

Upvotes: 0

ncica
ncica

Reputation: 7206

try:

test = 'google\\nfirefox'
import pprint
string = pprint.pformat(test) # google\\nfirefox
print (eval(string)) # google\nfirefox

output:

google\nfirefox

The eval() method parses the expression passed to this method and runs python expression (code) within the program.

Upvotes: -1

Aakash Mehta
Aakash Mehta

Reputation: 29

this is a really weird thing but i hope this works for you

test = 'google\\nfirefox'
pprint.pformat(test).split("\\")[0] + "\\" + pprint.pformat(test).split("\\")[2]

just some really wierd string manipulation.

Upvotes: -1

Related Questions