Files
30-seconds-of-code/snippets/byte_size.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

368 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
byte_size string,beginner 2018-02-01T10:19:59+02:00 2020-11-02T19:27:07+02:00

Returns the length of a string in bytes.

  • Use str.encode('utf-8') to encode the given string and return its length.
def byte_size(s):
  return len(s.encode('utf-8'))
byte_size('😀') # 4
byte_size('Hello World') # 11