You can look at sys.getsizeof. But it is more complicated than you may think.
You can look at sys.getsizeof. But it is more complicated than you may think.
int technically has a finite size, but if the result of a computation goes past that size, Python will automatically use an arbitrary-precision long instead. Those are only limited by available memory.
float is an IEEE 64-bit binary floating-point number. Many other languages would call that a double.
double and char don't exist in Python. Python only has one built-in floating-point data type, which is float. Single-character strings are used to represent characters.
What does it mean for the size of a char variable to be 1 byte?
How to get the size (length) of a string in Python - Stack Overflow
Python: Get size of string in bytes - Stack Overflow
Do there exist unicoded strings where len()/python and len()/rust are different?
Or any type of variable to be either size, I'm confused with the concept, does this mean I can't exceed 1 byte using that variable? Or it's something that adds to the final program size? If I use "double" and then "char" this mean I'm using 9 bytes of storage?
If you are talking about the length of the string, you can use len():
>>> s = 'please answer my question'
>>> len(s) # number of characters in s
25
If you need the size of the string in bytes, you need sys.getsizeof():
>>> import sys
>>> sys.getsizeof(s)
58
Also, don't call your string variable str. It shadows the built-in str() function.
Python 3:
user225312's answer is correct:
A. To count number of characters in str object, you can use len() function:
>>> print(len('please anwser my question'))
25
B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function
>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50
Python 2:
It gets complicated for Python 2.
A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object.
Sometimes it will be equal to character count:
>>> print(len('abc'))
3
But sometimes, it won't:
>>> print(len('йцы')) # String contains Cyrillic symbols
6
That's because str can use variable-length encoding internally. So, to count characters in str you should know which encoding your str object is using. Then you can convert it to unicode object and get character count:
>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols
3
B. The sys.getsizeof() function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object
>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32
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.
It’s 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 :).
You can use len(s.encode()), but there's a caveat.
The size in bytes of a string depends on the encoding you choose (by default "utf-8").
For some multi-byte encodings (e.g., UTF-16), string.encode will add a byte-order mark (BOM) at the start, which is a sequence of special bytes that inform the reader on the byte endianness used. So the length you get is actually len(BOM) + len(encoded_word).
If you don't want to count the BOM bytes, you can use either the little-endian version of the encoding (adding the suffix "-le") or the big-endian version (adding the suffix "be").
>>> len('ciao'.encode('utf-16'))
10
>>> len('ciao'.encode('utf-16-le'))
8
[SOLVED]
Hey, I am trying to create a password generator where I allow the user to enter the length of their password and I return to them a password of their desired length.
I am creating the password using the token_urlsafe() function from py's secrets module. But the function takes in byte length and not character length, so when I give it a length of 16, it gives me a password with 22 characters, this is obviously not user friendly so I want to find a way to convert the user's desired character length to byte length before sending it to token_urlsafe() so it creates a string with character length equal to what the user desires