Files
30-seconds-of-code/snippets/byte_size.md
2019-08-20 10:09:53 +03:00

300 B

title, tags
title tags
byte_size string

Returns the length of a string in bytes.

utf-8 encodes a given string, then len finds the length of the encoded string.

def byte_size(string):
    return(len(string.encode('utf-8')))
byte_size('😀') # 4
byte_size('Hello World') # 11