0

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.

user3151828
  • 363
  • 1
  • 7
  • 21
  • I don't believe your data there. `y = print x` would be a syntax error in python. I'm not exactly sure what you're really trying to accomplish here. What do you mean by "a string that prints something different than what it actually is"? Can you give a reproducible example? – mgilson Mar 12 '14 at 04:53
  • "string that prints something different than what it actually is" - can you show the example? I assume, it is problem with your code. – sashkello Mar 12 '14 at 04:53
  • Your question doesn't make sense. You already have the variable you need. If you print it, it looks as you expect. Why would you care about the format in which it is stored? If you just want to strip off linebreaks and special characters, that has been asked multiple times... But if you do it, your `print` will also look differently. – sashkello Mar 12 '14 at 05:04
  • @sashkello I require to type the string in a text field and it can't handle some of the characters. – user3151828 Mar 12 '14 at 05:06
  • @user3151828 what do you use for your output? Tkinter? – sashkello Mar 12 '14 at 05:09
  • @sashkello I know it is different because when I try to type the string in the text field: `UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 7: ordinal not in range(128)` – user3151828 Mar 12 '14 at 05:14
  • Try this: http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – sashkello Mar 12 '14 at 05:21

1 Answers1

1

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!

Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51