RGB to Hex Color

Easy · rating 1050 · Strings, Implementation, Math

Read three space-separated integers r g b (each 0–255). Print the color as a lowercase hex string #rrggbb, with each channel written as exactly two hex digits.

For example, 255 0 128 is #ff0080.

Editorial

Approach

Each channel is a byte (0–255) that maps to two hex digits. Format each with a zero-padded, two-width hex conversion and concatenate behind a #.

r, g, b = map(int, input().split())
print(f"#{r:02x}{g:02x}{b:02x}")

Watch out

The zero-padding matters: a channel value of 5 must render as 05, not 5, or the six-digit format breaks.

Complexity

Time: O(1). Space: O(1).

Related problems

Open RGB to Hex Color in Code Arena →