HOWTO · Python

How to Convert Integer to Roman Numerals in Python

This tutorial will teach you to convert an integer to Roman numerals in Python.

On this page

Roman numerals use seven symbols: I is 1, V is 5, X is 10, L is 50, C is 100, D is 500, and M is 1,000. A smaller symbol before a larger one indicates subtraction: IV is 4 and IX is 9. The same pattern gives XL (40), XC (90), CD (400), and CM (900). A greedy converter uses these combined forms so it does not produce invalid repetitions such as XXXX for 40.

Convert Integers to Roman Numerals in Python

The following function stores the symbol pairs from largest value to smallest. At each step, it appends a symbol while the remaining number is at least that pair’s value, then subtracts the value. It proceeds through the full mapping, so each value can be used repeatedly when needed. Its conventional Roman numeral input range is 1–3,999. The function does not validate that upper limit; above 3,999, it repeats M and can return a nonstandard numeral.

Run this example:

roman_map = [
    (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
    (100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
    (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I"),
]

def into_roman(num=42):
    result = ""
    for value, symbol in roman_map:
        while num >= value:
            result += symbol
            num -= value
    return result

print(into_roman())
XLII

This example converts 42 to XLII: the mapping first takes 40 as XL, then uses I twice for the remainder 2. Pass a different positive integer to into_roman(num) to convert another value in the conventional range. The function’s while loop appends a symbol repeatedly when a value occurs more than once; the outer for loop then considers smaller values. For 0 or a negative integer, the function returns an empty string because it only processes values while num is greater than zero. Roman numerals have no standard symbol for zero, so validate the range in your application if those inputs should be rejected.

Use Division to Convert Integers to Roman Numerals in Python

A second approach uses integer division and remainder. It visits the same 13 values in descending order. For each value, num // value gives the number of times its symbol fits; the remainder, num % value, becomes the value handled at the next step. This version produces the same numeral while making the quotient and remainder explicit. Like the first function, it is intended for the conventional range 1–3,999 and does not enforce the upper limit; above 3,999, it repeats M and can return a nonstandard numeral.

roman_map = {
    1000: "M", 900: "CM", 500: "D", 400: "CD",
    100: "C", 90: "XC", 50: "L", 40: "XL",
    10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I",
}
order = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

def into_roman_by_division(num=42):
    result = ""
    for value in order:
        quotient, num = divmod(num, value)
        result += roman_map[value] * quotient
    return result

print(into_roman_by_division())
XLII

For 42, the 40 entry has quotient 1 and leaves 2; the 1 entry then contributes two I symbols, giving XLII. The loop still visits every value even when its quotient is 0, and divmod updates the remainder for the next value. As in the first function, this implementation is intended for positive integers: with 0 it returns an empty string, and with a negative integer Python’s floor division does not represent Roman numeral subtraction. Check that the value is in the supported range before calling either function when handling external input.