How a string size is calculated in python? I tried a below code:
s = "test"
s.__sizeof__()
53
bytes(s, "utf-8").__sizeof__()
37
bytes(s, "utf-16").__sizeof__()
43
bytes(s, "utf-32").__sizeof__()
53
How does python calculate the size for a string? Even if I consider, utf-8 encoding, any character can take anywhere between 1 byte to 4 bytes. Even if I consider the maximum size of 4 bytes per character, a string of 4 characters should take around 16 bytes, but __sizeof__
function shows bytes ranging from 37 bytes to 53 bytes based on the encoding chosen.