+

How do I get the size of a string in Python?

Python has a len () method that gives us the length of any compound object. To get the length of a string, simply pass the string to len ().



Example

 print (len (`Hello World!`)) 

Output

 12 

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

Example

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

Output

 33 



Get Solution for free from DataCamp guru