Files
30-seconds-of-code/snippets/python/s/byte-size.md
2023-05-07 16:07:29 +03:00

385 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Byte size of string snippet python
string
river-house-lights 2020-11-02T19:27:07+02:00

Returns the length of a string in bytes.

  • Use str.encode() 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