What is the difference between <class 'str'> and <type 'str'>

I am new to python. I am confused <class 'str'>. I got a string using:

response = urllib.request.urlopen(req).read().decode()

The type of response is <class 'str'>, rather than <type 'str'>. When I try to manipulate this line in 'for loop':

for ID in response: 

The "answer" is read NOT by line, BUT by character. I intend to put each line of the "response" in a separate list item. Now I have to write the answer in the file and use 'open' to get a string <type 'str'>that I can use in 'for loop'.

+4
source share
2 answers

As mentioned by commentators. In python3:

>>>st = 'Hello Stack!'
>>>type(st)
<class 'str'>

But in python2:

>>>st = 'Hello Stack!'
>>>type(st)
<type 'str'>

, , , . , for . , - split \n , URL. split

response = urllib.request.urlopen(req).read().decode()
lines = response.split('\n')
for x in lines:
    st = x.strip()
    # do some processing on st
0

. Python type python 2 ( : <type 'int'>.) python 3 ( : <class 'int'>.). python 2 3 : um, type:

python 2

>>> type(type('a'))
<type 'type'>

python 3

>>> type(type('a'))
<class 'type'>

... , .

,

for ID in response:

response - , . , , HTML, JSON , python.

+3

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


All Articles