Byte size of string

Python, String · Nov 2, 2020

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

More like this

  • Pad string

    Pads a string on both sides with the specified character, if it's shorter than the specified length.

    Python, String · Oct 3, 2020

  • How do I trim whitespace from a string in Python?

    Oftentimes you might need to trim whitespace from a string in Python. Learn of three different way to do this in this short guide.

    Python, String · Dec 13, 2021

  • Pad number

    Pads a given number to the specified length.

    Python, String · Nov 2, 2020