Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...