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

Answer from user225312 on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_string_length.asp
Python String Length
The len() function returns the length of a string: a = "Hello, World!" print(len(a)) Try it Yourself » · Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative ...
🌐
Flexiple
flexiple.com › python › python-string-length
Python String Length - Flexiple
When applied to a string, len() ... be they alphabetic, numeric, symbols, or whitespace. example_string = "Hello World" print(len(example_string)) # Output: 11...
People also ask

How does Python handle the length of Unicode strings?
Python's len() function accurately counts the number of Unicode characters, even if they are represented by multiple bytes. For example, len("") returns 1, as it counts the emoji as a single character.
🌐
metaschool.so
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
How can I find the longest and shortest strings in a list?
Use max() and min() with the key=len argument. For example:
longest = max(strings, key=len)
shortest = min(strings, key=len)

This efficiently identifies the longest and shortest strings based on length.
🌐
metaschool.so
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
What should I do if my string slicing isn’t working as expected?
Ensure your slice indices are within the string's length. Python handles out-of-bound indices gracefully. For example, text[0:20] on a short string will return the entire string without error.
🌐
metaschool.so
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-length-len
Python string length - GeeksforGeeks
October 15, 2024 - In this example, we are using Python len() with dictionaries and sets to find the length of dictionary and set.
Top answer
1 of 7
322

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

2 of 7
49

Python 3:

user225312's answer is correct:

A. To count number of characters in str object, you can use len() function:

>>> print(len('please anwser my question'))
25

B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function

>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50

Python 2:

It gets complicated for Python 2.

A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object.

Sometimes it will be equal to character count:

>>> print(len('abc'))
3

But sometimes, it won't:

>>> print(len('йцы'))  # String contains Cyrillic symbols
6

That's because str can use variable-length encoding internally. So, to count characters in str you should know which encoding your str object is using. Then you can convert it to unicode object and get character count:

>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols 
3

B. The sys.getsizeof() function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object

>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32
🌐
Tutorialspoint
tutorialspoint.com › home › python › python string length
Python String Length
February 21, 2009 - For example, the string "Tutorials Point" has 15 characters in it including the space. Following is the syntax of Python String len() method: ... This method does not accept any parameter. This method returns the length of the string.
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Since strings are arrays, we can loop through the characters in a string, with a for loop. ... Learn more about For Loops in our Python For Loops chapter. To get the length of a string, use the len() function.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-string.html
Python Strings
The str() function serves to convert many values to a string form. Here is an example this code computes the str form of the number 123: ... Looking carefully at the values, 123 is a number, while '123' is a string length-3, made of the three chars '1' '2' and '3'.
Find elsewhere
🌐
Metaschool
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
December 6, 2024 - To find out string length in Python, you can use the len() function. This built-in function returns the number of characters in the string, including letters, numbers, spaces, and punctuation.
🌐
StrataScratch
stratascratch.com › blog › how-to-determine-python-string-length
How to Determine Python String Length? - StrataScratch
August 1, 2024 - Let’s say you want to measure the length of the strings in Python, but how? Meet len(), your new best friend, for string measurements.
🌐
AlgoCademy
algocademy.com › link
String Length in Python | AlgoCademy
In this lesson, we covered the basics of determining string length in Python using the len() function. We explored how to access and slice characters from the end of a string, provided practical examples and use cases, discussed common pitfalls ...
🌐
Board Infinity
boardinfinity.com › blog › len-string-in-python
Python String Length: How to Find It? | Board Infinity
April 2, 2025 - Python provides many functions to count Python string length, the simplest and most frequently used is len() function. It is an inbuilt function that counts the number of characters in a string in terms of space and even special characters. In the example below you will see how you have to ...
🌐
Real Python
realpython.com › len-python-function
Using the len() Function in Python – Real Python
November 16, 2024 - For this toy class, you define the object’s length as the number of occurrences of the letter Y in the string. Therefore, the .__len__() method returns the count of the letter Y. You can create an object of class YString and find its length. The module name used for the example above is ystring.py: ... >>> from ystring import YString >>> message = YString("Real Python?
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to find the length of a string in python
Find Length of a String in Python - 5 Easy Methods
December 18, 2025 - The length of a string in Python refers to the number of characters in the string or the size of the string. For example, the length of the string “Interview” is 9 because it has nine characters.
🌐
LinkedIn
linkedin.com › all › engineering › programming
How do you determine the length of a string in Python?
January 16, 2024 - The str.__len__() method returns ... rather than as a function on the argument. For example, you can get the length of a string variable called name by calling name.__len__()....
🌐
Educative
educative.io › answers › how-to-find-the-length-of-a-string-in-python
How to find the length of a string in Python
It takes a string as a parameter and returns an integer as the length of that string. For example, len(“educative”) will return 9 because there are 9 characters in “educative”.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python string length with examples
Python String length with Examples - Spark By {Examples}
May 31, 2024 - # Consider the string string1 = ... print("String Length: ",it) # Output: # String: sparkby examples - Python tutorials # String Length: 35...
🌐
Unstop
unstop.com › home › blog › python string length | 10 ways explained with code examples
Python String Length | 10 Ways Explained With Code Examples
January 14, 2025 - Return Value: The function returns an integer representing the total number of elements in the object. The simple Python program example below illustrates how to use the len() function to find the length of a string.
🌐
freeCodeCamp
freecodecamp.org › news › what-is-len-in-python-how-to-use-the-len-function-to-find-the-length-of-a-string
What is len() in Python? How to Use the len() Function to Find the Length of a String
February 24, 2022 - This means there are 60 characters in the string. You might be wondering how the len() function works on other data types such as lists and tuples. When you use the len() function on a data type like tuple or list, it returns the number of items ...
🌐
LabEx
labex.io › tutorials › python-how-to-modify-string-length-in-python-419449
How to modify string length in Python | LabEx
In Python, strings are sequences of characters, and their length can be easily determined using the built-in len() function.