xin.chen
xin.chen

Reputation: 986

how to loop through the range of \u2190-\u21FF in Python2

I want to know \u2190-\u21FF what is that?

how to loop through the range of \u2190-\u21FF in python2?

unicode \u2190-\u21FF

my only be parsed \u0030-\u0039

for i in range(0x30,0x38):
    print chr(i)
# 1,2,3,4,5,6,7,8

parsing \u2190-\u21FF is not supported

and How to calculate the hexadecimal of 0039?

print hex(0039)
SyntaxError: invalid token

Upvotes: 1

Views: 74

Answers (2)

lenik
lenik

Reputation: 23556

this works for me:

$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
>>> for i in range(0x3000,0x303F):
...     print unichr(i)
... 
 
、
。
〃
〄
々
〆
〇
〈
〉
《
》
「
」
『
』
【
】
〒
〓
〔
〕
〖
〗
〘
〙

and 0039 has leading zero, which makes it an octal constant, hence the number 9 is forbidden there. Please, make sure you're converting correct numbers.

Upvotes: 1

Madlozoz
Madlozoz

Reputation: 367

This code works perfectly with Python 3.6

Edit: I added the Python 2.7 tag to the original question

for i in range(0x3000,0x303F):
    print (chr(i))

The print hex(0039) problem is trickier

Python do not accept a literal integer with leading 0. But it can convert a string with leading 0 to integer.

This means that you are simply not allowed to write hex(0039) or even int(0039) but why would you? hex(39)works just fine.

And if you deal with string, there is no problem as long as you convert it as int

s = '0039'
print  (hex(int(s)))

Upvotes: 1

Related Questions