Python string manipulation

I have a python file like this

import urllib2

try:
    data = urllib2.urlopen('http:....').read()
except urllib2.HTTPError, e:
    print "HTTP error: %d" % e.code
except urllib2.URLError, e:
    print "Network error: %s" % e.reason.args[1]

print data1

The output is as follows:

>>>
15.95

>>>

I need to perform some manipulations with data1 (or any alternative variable) so that when printing data1 (or a new variable) there is no extra line in the output. In other words, I want it to look like this:

>>>
15.95
>>>

Any help would be great. I am relatively new to python. I spun with \ r and can't make it work.

+3
source share
4 answers

Perhaps you just need to remove the spaces from your variable? Use data1.strip(). Read str.strip () for more details .

By the way, to clearly see the space, use print repr(data1).

+7
source
+1

you can also use rstrip () if all you want to do is get rid of unnecessary things on the right side

0
source

Source: https://habr.com/ru/post/1786032/


All Articles