How to allow special characters in Python?

Question:

In my program I want to include characters like these in the print: ╣, ╠, ╬, and a few more like that. I'm trying to take an array of numbers and transform them into these characters so that it looks like a map/maze.

Obs: I want a position [x][y] of the matrix to receive this character as content.

for i in range(10):
    for j in range(10):
        if matriz[i][j] == 0:
            sqm[i][j] = " "
        elif matriz[i][j] == 1:
            sqm[i][j] = "0"
        elif matriz[i][j] == 2:
            sqm[i][j] = u("U+25AC")
for i in range(10):
    for j in range(10):
        print sqm[i][j],
    print ""

This way it needs to be already included within the matrix.

At the beginning of the code I already use:

# -*- coding: utf-8 -*-

But it only covers accents. How do I solve this?

Answer:

To do this, just look for the unicode characters you want to "print" and use the command:

print(u'\u2551')
║

Example:

box = [u'\u2550',u'\u2551',u'\u2552',u'\u2553',u'\u2554',u'\u2555',u'\u2556',
u'\u2557',u'\u2558',u'\u2559',u'\u225A']

print(box)
['═', '║', '╒', '╓', '╔', '╕', '╖', '╗', '╘', '╙', '≚']

You can see the full table of characters called "box drawing" at this link. , or if you need to (and have patience) you can check all the unicode tables here.

Run the code in repl.it.

Edited:
After the question author edited it and entered the code.

Looking at the code, you can see the error exactly where the objective is to assign the character to the matrix position: sqm[i][j] = u("U+25AC") , probably this 'notation' must have been deduced from this table , or some similar. But when the table shows U+25AC , the conversion to python should become u'\u25ac' , so just change the line:

sqm[i][j] = u("U+25AC")

For:

sqm[i][j] = u'\u25ac'

See this character added to the table in the new version, on repl.it.

Scroll to Top