user3151828
user3151828

Reputation: 363

How to copy what Python last printed and put it to a string

The reason for this is for a string that prints something different than what it actually is, which I have strangely encountered. So what I want is something that gives an output like:

>>>x = "hi"
>>>print x
hi
>>>y = print x
>>>y
hi

And what I mean is I already have this huge code where it gets info from html but if I do:

print(Density_[i])

I get the string that I want to have, but if I just do:

>>> Density_[i]

I get a totally different thing that still contains elements of some \n line breaks and some \xs.

Basically what I want to do is access the last output and store it in a variable.

Upvotes: 0

Views: 2440

Answers (1)

Sharif Mamun
Sharif Mamun

Reputation: 3554

Only this is possible:

>>> x = 'hi'
>>> print (x)
hi

>>> y = '%s' %x
>>> y
'hi'
>>> 

If you print the string, the ' ' will be gone and it's totally safe to assign to another variable.

To remove extra stuffs, use:

y = x.strip()

Hope, this would solve your issues.

Cheers!

Upvotes: 1

Related Questions