Search code examples
pythonspecial-charactersstring-literals

Where does "\N{SPECIAL CHARACTER}" in Python come from?


I came across some urwid tutorial, which included an example with code such as this:

...
main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
    align='center', width=('relative', 60),
    valign='middle', height=('relative', 60),
    min_width=20, min_height=9)
urwid.MainLoop(top, palette=[('reversed', 'standout', '')]).run()

That u'\N{MEDIUM SHADE}' string literal drove me nuts for almost the entire day until I found out it was included — as comments! — in files under /usr/lib/python3.5/encodings/... But nowhere did I find any hint as to using such a notation. I browsed Python documentation and could find nothing.

Out of curiosity I ran in my python interpreter:

print(u'\N{LOWER ONE QUARTER BLOCK}')

and I got

Where does that kind of black magic come from? I mean, where is it explained one can use that... notation (?) to print out special characters using their friendly names? Does Python hide any other surprises like this one?


Solution

  • Towards the end of https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals:

    \N{name} - Character named name in the Unicode database