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: 13Key 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.
What is the max length of a Python string? - Stack Overflow
Understanding String Length in Different Programming Languages
How does Python interpret a zero length string?
A Simple struct-like Module with Variable-Length Strings, Intended for Networking
Videos
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.
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
With a 64-bit Python installation, and (say) 64 GB of memory, a Python string of around 63 GB should be quite feasible, if not maximally fast. If you can upgrade your memory beyond 64 GB, your maximum feasible strings should get proportionally longer. (I don't recommend relying on virtual memory to extend that by much, or your runtimes will get simply ridiculous;-).
With a typical 32-bit Python installation, the total memory you can use in your application is limited to something like 2 or 3 GB (depending on OS and configuration), so the longest strings you can use will be much smaller than in 64-bit installations with high amounts of RAM.
I ran this code on an x2iedn.16xlarge EC2 instance, which has 2048 GiB (2.2 TB) of RAM
>>> one_gigabyte = 1_000_000_000
>>> my_str = 'A' * (2000 * one_gigabyte)
It took a couple minutes but I was able to allocate a 2TB string on Python 3.10 running on Ubuntu 22.04.
>>> import sys
>>> sys.getsizeof(my_str)
2000000000049
>>> my_str
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
The last line actually hangs, but it would print 2 trillion As.