To find the length of a string in Python, use the built-in len() function. It returns the number of characters in the string, including letters, digits, punctuation, spaces, and special characters.

text = "Hello, World!"
length = len(text)
print(f"The length of the string is: {length}")  # Output: 13

Key Points:

  • len() works on strings, lists, tuples, dictionaries, and other sequences.

  • It counts every character, including whitespace and punctuation.

  • For Unicode strings (e.g., containing non-ASCII characters like "世" or emojis), len() returns the number of characters, not bytes.

  • To get the byte size of a string, encode it first:

    byte_length = len(text.encode('utf-8'))

Alternative Methods (for educational purposes):

  • Using a loop with a counter:

    count = 0
    for _ in text:
        count += 1
    print(count)
  • Using sum() with a generator:

    length = sum(1 for _ in text)

While these alternatives demonstrate how iteration works under the hood, len() is the most efficient and Pythonic way to get string length.

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 ...
🌐
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.
Discussions

What is the max length of a Python string? - Stack Overflow
If it is environment-independent, what is the theoretical maximum number of characters in a Python string? More on stackoverflow.com
🌐 stackoverflow.com
Understanding String Length in Different Programming Languages
The real challenge is that there is no universally correct atomic unit of decomposition for strings, which means that string length is itself incoherent. And likewise there can be no universal character type. How long is 밥 for example? Is it one character or three? It depends on how you're looking at it. Text processing is much more interesting than the illusion of simplicity our languages tend to provide. More on reddit.com
🌐 r/programming
15
5
April 23, 2025
How does Python interpret a zero length string?
Is it because the string terminator is matching? No, it's because the whole string matches - "in" for strings searches for any matching substring. As such, there's a zero length string (indeed, an infinite number of them) between every character in every string, and so conceptually, a zero length string matches everything. (ie. "abc" == "a" + "" + "" + "" + "b" + "" + "c"). It's nothing to do with a terminator, but ultimately with what "contained within" means, and how this relates to the base case. So what does "contained within" mean exactly? Essentially, it means that every character in the string must appear, in order, in the string we're checking. Here, we've got a degenerate case where there are no such characters, which in fact means that this check passes - every single character (all none of them) appears in order in the string we're checking. There is no character in the ASCII chart matching here, and that's exactly why the string matches. You can consider it similar to similar constructions in logic. Eg. consider something like "Every occurrance of the 26th letter of the alphabet in this sentence is purple". That happens to be a true statement, even though there are no cases of purple "z"s in it, because there are no "z"s at all - meaning every single one of them was purple (and orange, and green, or any other claim, all at once). That can sometimes seem a bit counterintuitive, but it's an important base case to consider, and should hopefully illustrate why "Every letter in the empty string is contained in 'String' is true". More on reddit.com
🌐 r/Python
5
0
December 1, 2014
A Simple struct-like Module with Variable-Length Strings, Intended for Networking
A friend and I have been developing a networking library for Python for more than a year now, and one of the features I'm excited about is an easy way to read and write binary wire protocols. The problem is... struct felt lacking to me. It's quite good at what it does, but its string handling made it unnecessarily hard to use. This is my attempt to solve that. NetStruct puts logic on top of struct that makes it possible to pack and unpack data with length encoded strings. I won't put any examples here, but the readme on github has several. More on reddit.com
🌐 r/Python
13
11
October 26, 2012
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
🌐
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'.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
6 days ago - Since there is no separate “character” type, indexing a string produces strings of length 1.
🌐
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.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python maximum string value length
Python Maximum String Value Length - Spark By {Examples}
May 21, 2024 - The maximum length of a string depends on the underlying system and available resources. However, the theoretical maximum length of a string in Python is sys.maxsize, which is the maximum value for a signed integer on the platform.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string
Python String - GeeksforGeeks
Python does not have a separate character type. A single character is treated as a string of length one.
Published   January 14, 2026
🌐
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
1 week ago - 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.
🌐
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.
🌐
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.
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
This is used by vformat() to break the string into either literal text, or replacement fields. The values in the tuple conceptually represent a span of literal text followed by a single replacement field. If there is no literal text (which can happen if two replacement fields occur consecutively), then literal_text will be a zero-length string.
🌐
TutorialsPoint
tutorialspoint.com › what-is-the-maximum-length-of-string-in-python
What is the maximum length of string in Python?
May 29, 2025 - Python strings are stored as sequences of Unicode characters, and the maximum size of a Python object is typically constrained by the value of sys.maxsize attribute, which reflects the platform's pointer size. Following is the example which shows how to retrieve the theoretical maximum string length using sys.maxsize attribute -
🌐
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 ...
🌐
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").
🌐
CodeChef
codechef.com › learn › course › python › LTCPY08 › problems › PYTH40
String length in Python Programming
Test your Learn Python Programming knowledge with our String length practice problem. Dive into the world of python challenges at CodeChef.
🌐
Reddit
reddit.com › r/programming › understanding string length in different programming languages
r/programming on Reddit: Understanding String Length in Different Programming Languages
April 23, 2025 - Python strings are sequences of Unicode code points, and Javascript strings are sequences of UTF-16 code units, no?
🌐
Python Forum
python-forum.io › thread-24349.html
How can i limit length string?
February 10, 2020 - I use this script in my zabbix-server for rabbitMQ https://github.com/jasonmcintosh/rabbitm...tmq/api.py And names on my rabbitMQ-server queues are long, more then 128 symbols and i ...
🌐
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.