Start free

Intermediate · Text processing · 154 of 264

Roman numerals

Convert a Roman numeral to a number. I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A smaller value before a larger one is subtracted (IV is 4).

Input: a Roman numeral. Output: its value.

Examples

Input
XIV
Output
14
Input
MMXXIV
Output
2024
Input
IX
Output
9

2 more tests check other inputs when you press Check.

Stuck? The idea behind text processing

Go through text a character at a time, and build the answer as you go: a new string, or a list of pieces joined at the end with "".join().

ord() gives a character's number and chr() the character for a number: useful for shifting letters.

text = input()
out = []
for ch in text:
    out.append(ch.upper() if ch.islower() else ch.lower())
print("".join(out))

Watch for: Shifting letters past "z" must wrap round to "a": use % 26 on the letter's position in the alphabet.

All the text processing exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More text processing exercises