Characters and bytes

A message goes onto the wire as bytes, and with UTF-8 the byte count and the character count can differ.

What to build

A program that prints how many characters and how many UTF-8 bytes a line of text has.

Requirements

  • Print chars N, the number of characters.
  • Print bytes M, the number of bytes when the text is encoded as UTF-8.

Input

One line of text (it may contain spaces and non-English letters).

Output

Two lines: chars N and bytes M.

Constraints

The line has 0 to 200 characters.

Example

Input:

hello

Output:

chars 5
bytes 5

Why it works

UTF-8 uses 1 byte for plain English letters and 2, 3 or 4 bytes for other characters, so a length prefix has to use the byte count, which is what encode and len give.

Examples

Example 1 · Input
hello
Expected output
chars 5
bytes 5
Example 2 · Input
café €5
Expected output
chars 7
bytes 10

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

len(text) counts characters.

Hint 2

text.encode("utf-8") gives the bytes; len() of that counts bytes.

Hint 3

Read the line with input() and do not strip away anything except the newline.

Solution

Show one correct solution (try it yourself first)
text = input()
print("chars", len(text))
print("bytes", len(text.encode("utf-8")))

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Sockets and framing: messages on a byte stream →