I have a string that is to be sent over a network. I need to check the total bytes it is represented in.
sys.getsizeof(string_name)
returns extra bytes. For example for sys.getsizeof("a")
returns 22 , while one character is only represented in 1 byte in python. Is there some other method to find this ?
If you want the size of the string in bytes, you can use the getsizeof() method from the sys module.
1) s. length() will give you the number of bytes. Since characters are one byte (at least in ASCII), the number of characters is the same as the number of bytes.
You can get the length of a string object by using a size() function or a length() function. The size() and length() functions are just synonyms and they both do exactly same thing.
Python bytes() Function The bytes() function returns a bytes object. It can convert objects into bytes objects, or create empty bytes object of the specified size.
If you want the number of bytes in a string, this function should do it for you pretty solidly.
def utf8len(s): return len(s.encode('utf-8'))
The reason you got weird numbers is because encapsulated in a string is a bunch of other information due to the fact that strings are actual objects in python.
Its interesting because if you look at my solution to encode the string into 'utf-8', there's an 'encode' method on the 's' object (which is a string). Well, it needs to be stored somewhere right? Hence, the higher than normal byte count. Its including that method, along with a few others :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With