1

I have a string, and I want to add unique non-ASCII characters to it. I need to do it in a loop because I may need to add more than one. The problem is that I don't know how to construct a proper Unicode string in the loop.

For example, I would like to add \u2713, \u2714, \u2715 etc to my string. I'm not sure how to do it.

s = 'ABCD'

for j in range(10):        
    s = s + u'\u2713'
    #s = s + (u'\u2713' + j) # This doesn't work

print s
dda
  • 6,030
  • 2
  • 25
  • 34
ABCD
  • 7,914
  • 9
  • 54
  • 90
  • @JeremyBanks Copy and paste error. Fixing now... – ABCD Mar 16 '16 at 06:08
  • Maybe [Python unicode codepoint to unicode character](http://stackoverflow.com/questions/10715669/python-unicode-codepoint-to-unicode-character)? – Jeremy Mar 16 '16 at 06:10

1 Answers1

4

You can use unichr (chr in Python 3.x) to convert int to unicode string:

s = 'ABCD'

for i in range(10):
    s += unichr(0x2713 + i)

print s

prints ABCD✓✔✕✖✗✘✙✚✛✜


Instead of appending characters, you can use str.join (or unicode.join):

s = 'ABCD' + u''.join(unichr(0x2713 + i) for i in range(10))

OR

s = 'ABCD' + u''.join(unichr(ch) for ch in range(0x2713, 0x271d))

OR

s = 'ABCD' + u''.join(map(unichr, range(0x2713, 0x271d)))
falsetru
  • 357,413
  • 63
  • 732
  • 636