0 votes
1 view
in Python by (40.4k points)

For example, I get a string:

str = "please answer my question"

I want to write it to a file.

But I need to know the size of the string before writing the string to the file. What function can I use to calculate the size of the string?

1 Answer

0 votes
by (92.9k points)

If you want to get the length of the string, you can use len():

>>> s = 'intellipaat community' 

>>> len(s) 

If you need the size of the string in bytes, you need sys.getsizeof():

>>> import sys 

>>> sys.getsizeof(s) 

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...