How to get the size (length) of a string in Python

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)