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
Remove List Duplicates Reverse ... Bootcamp Python Certificate Python Training ... To get the length of a string, use the len() function....
🌐
Tutorialspoint
tutorialspoint.com › home › python › python string length
Python String Length
February 21, 2009 - The Python String len() method is used to retrieve the length of the string.
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 - It returns an integer which is the length of the string. The len() method is used to find the length of an object in Python. By using len(), we can find the length of strings and lists.
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
🌐
Flexiple
flexiple.com › python › python-string-length
Python String Length - Flexiple
In this example, len(my_string) returns 13, which is the number of characters in the string "Hello, Python!". The len() function in Python is versatile and can be used to determine the length of various data types, including strings and tuples.
🌐
Educative
educative.io › answers › how-to-find-the-length-of-a-string-in-python
How to find the length of a string in Python
To calculate the length of a string in Python, you can use the built-in len() method.
🌐
Metaschool
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
December 6, 2024 - Learn how to use Python's len() function to determine string length. Explore practical examples and use cases for validation, text manipulation, and more.
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_func_len.asp
Python len() Function
When the object is a string, the len() function returns the number of characters in the string. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want ...
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › python › python size length
How to Get the String Length and Size in Python | Delft Stack
March 11, 2025 - Using len() is straightforward and efficient, making it the go-to method for measuring string lengths in Python.
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › find-length-of-a-string-in-python-4-ways
Find Length of String in Python - GeeksforGeeks
December 3, 2024 - a = "geeks" #Initialize a variable 's' #to keep track of the string's length s = 0 for i, a in enumerate(a): # Increment 's' by 1 for each character in the string s += 1 print(s) ... A string is a sequence of characters. Python treats anything inside quotes as a string.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Length of a String (number of Characters) in Python | note.nkmk.me
April 30, 2025 - In Python, you can get the length of a string (str), which is the number of characters, using the built-in len() function. Built-in Functions - len() — Python 3.13.3 documentation Get the length of a ...
🌐
Real Python
realpython.com › len-python-function
Using the len() Function in Python – Real Python
November 16, 2024 - The len() function in Python returns the number of items in an object, such as strings, lists, or dictionaries. To get the length of a string in Python, you use len() with the string as an argument, like len("example").
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › string length python
String Length Python | How to Find Length of String in Python?
October 9, 2023 - We’ll also delve into examples ... and escape effectively. ... In Python, you can find the length of a string using the len() function....
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Cisco
ipcisco.com › home › python string length
Python String Length | How to Calculate String Lenght in Python?⋆ IpCisco
December 27, 2021 - Let’s do an example about python sting length wşth len () function. message = "hello,how are you?" print(len(message)) As you can see above, we have a message and this message has 18 characters including signs and spaces.
🌐
STechies
stechies.com › python-string-length-len
Python String Length | len() Method with Example
stringlenth = 'Stechies' print("String Length: ",len(stringlenth)) ... #!/usr/bin/python listB, listD = [9977, 'xyz', 'sahara', 'stechies'], [459, 'pqr', 234] print("Length of list B : ", len(listB)) print("Length of list D : ", len(listD))
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-string.html
Python Strings
>>> s = 'Python' >>> len(s) 6 >>> len('') # empty string 0 · The formal name of the string type is "str". 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'.
🌐
Replit
replit.com › home › discover › how to find the length of a string in python
How to find the length of a string in Python
In the example, len() is used on the string text. It's important to note what it counts: Every character, from letters to punctuation. Whitespace characters, like the space after the comma. This is why the result is 13. Because len() is a built-in function, it's incredibly efficient and the most Pythonic way to get an object's length.