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 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
• 57,700 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)


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

best python training in Delhi

answered Mar 29, 2019 by rajesh
• 1,230 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,400 points
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
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 in Python by Niroj
• 77,360 points
+1 vote
2 answers

How to print first character of each word in upper case of a string in Python

class Solution:     def firstAlphabet(self, s):             self.s=s              k=''              k=k+s[0]              for i in range(len(s)):                     if ...READ MORE

answered Oct 28 in Python by Anurag
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,561 views
0 votes
0 answers
+4 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,800 points
0 votes
3 answers

How to get the current time in Python


answered Apr 8, 2019 in Python by rajesh
• 1,230 points
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,230 points