White Cheese (Burgos) - Developing
White Cheese (Burgos) - Developing
Very new or simple note, likely to evolve and change.
Click the cheese icon to learn more

Get Bytes of a String in Python

Author: guiferviz

Created:

Last Modified:

In Python, you can determine the total size of a string in bytes by first encoding the string to Unicode Transformation Format 8-bit (UTF-8) and then using the len function. This is necessary because, by default, strings in Python 3 are Unicode, where a character can occupy more than one byte.

>>> len('你')
1
>>> len('你'.encode('utf-8'))
3