Article 58WE0 Python one-liner to print Roman numerals

Python one-liner to print Roman numerals

by
John
from John D. Cook on (#58WE0)

Here's an amusing little Python program to convert integers to Roman numerals:

 def roman(n): print(chr(0x215F + n))

It only works if n is between 1 and 12. That's because Unicode contains characters for the Roman numerals I through XII.

Here are the characters it produces:

You may not be able to see these, depending on what fonts you have installed. I can see them in my browser, but when I ran the code above in a terminal window I only saw a missing glyph placeholder.

If you can be sure your reader can see the characters, say in print rather than on the web, the single-character Roman numerals look nice, and they make it clear that they're to be interpreted as Roman numerals.

Here's a screenshot of the symbols.

roman.png

The post Python one-liner to print Roman numerals first appeared on John D. Cook.

6rXvQQWNQJk
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments