How to find the size of a string in Python

–1 vote
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?
Aug 6, 2018 in Python by bug_seeker
• 15,510 points
2,105 views

2 answers to this question.

–1 vote
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 str. It shadows the built-in str() function.

Hope this would help you @bug_seeker
answered Aug 6, 2018 by Priyaj
• 58,020 points
0 votes

following way to find length of string 

x = input("Enter a string:")
counter = 0 # count the character in string 
for s in x:
      counter = counter+1
print("Length of the input string is:", counter)

output

Enter a string: rajesh
Length of the input string is:6

i want to say somethings string is very important in python so if you know more about python and string concept in python than you can join

answered Mar 29, 2019 by rajesh
• 1,270 points
Hey @rk, consider this to be the last warning. Please Refrain from posting promotional content on the Community. It is not relevant and we'd like to keep the community clean.

Related Questions In Python

0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,373 views
0 votes
1 answer

How to find the value of a row in a csv file in python?

If you want to find the value ...READ MORE

answered May 20, 2019 in Python by Sanam
19,216 views
0 votes
1 answer

How to find the value of a row in a csv file in python and print column and row of that value ?

Hello @Khanhh , Use panda to find the value of ...READ MORE

answered Oct 15, 2020 in Python by Niroj
• 82,840 points
6,767 views
0 votes
0 answers

How can I find the first occurrence of a sub-string in a python string?

I'd like to locate the first index ...READ MORE

Nov 24, 2022 in Python by sarit
• 1,830 points
573 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,067 views
0 votes
3 answers

How to get the current time in Python

FOLLOWING WAY TO FIND CURRENT TIME IN ...READ MORE

answered Apr 8, 2019 in Python by rajesh
• 1,270 points
2,074 views
0 votes
2 answers

How do I connect to a MySQL Database in Python?

connect mysql database with python import MySQLdb db = ...READ MORE

answered Mar 28, 2019 in Python by rajesh
• 1,270 points
1,883 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP