String Encode in Python

Level 260 keystrokes

This encodes a string to bytes using UTF-8 encoding.

Python code

# Create a normal (Unicode) string
1text = "hello"
# encode("utf-8") converts the string into a bytes object using UTF-8
# For ASCII letters like these, each character becomes one byte
2encoded = text.encode("utf-8")
# Prints b'hello' (the b prefix marks a bytes object)
3print(encoded)

Output

b'hello'
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • "× 4
  • (× 2
  • )× 2
  • =× 2
  • -× 1
  • .× 1

More Python examples at level 2

← Python typing practice