Computer >> Computer tutorials >  >> Programming >> Python

How to get the size of a string in Python?


Python has a method called len() that gives us the length of any composite object. To get the length of a string, just pass the string to the len() call. 

 example

print(len('Hello World!'))

Output

12

If you want the size of the string in bytes, you can use the getsizeof() method from the sys module. 

example

from sys import getsizeof
getsizeof('Hello World!')

Output

33

You can read more about it at sys.getsizeof()