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

Find the Length Of String Without Using len Function In Python
When you ask something like this its either because you dont understand what you are doing or you have some very specific requirement. Either way you have to explain why you are trying this for us to be able to help. Since you didnt, ill just go ahead and post my bad solution print(str) Count the length of it by hand Done More on reddit.com
🌐 r/programming
3
0
December 6, 2021
is string.find(substring) time complexity of O(n) or (O*m)?
It doesn't seem to be specified anywhere but I would expect it to be O(n×m) where n is the length of string and m is the length of substring. More on reddit.com
🌐 r/cpp_questions
5
4
May 14, 2019
How to limit length of (.*?) match?
Yeah, basically, instead of *, you'd use your limit braces. src="(.{0,100})" But in your case, you'd probably want to change your dot to be a negated character class, like this so that it's not also matching quotes: \bsrc="([^"]{0,100})" Here is a demo More on reddit.com
🌐 r/regex
4
3
May 28, 2020
TypeError: ord() expected string of length 1, but int found
In python3, when reading files in binary mode, the ord conversion is already done. That function in python3 would be: def read_multi(f, length=2, reverse=True): vals = f.read(length) if reverse: vals = reversed(vals) value = 0 for val in vals: value = value << 8 value = value | val return value Or, much cleaner, use the built in int class. What your code calls reverse is called "endianess" in the rest of the world: def read_multi(f, length=2, reverse=True): vals = f.read(length) endian = 'little' if reverse else 'big' return int.from_bytes(vals, endian) Note this all only works in binary mode. More on reddit.com
🌐 r/learnpython
2
1
March 16, 2019
🌐
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.
🌐
Python Examples
pythonexamples.org › python-string-length-example
How to get Python String Length? Examples
In this example, we will take a string constant and find its length using len(). mystring = 'python examples' #length of string length = len(mystring) print('Length of the string is:', length)
🌐
Codefinity
codefinity.com › courses › v2 › 04fb3c65-4043-4ee3-a995-cba5bdd807af › b427d60d-9690-43e9-8110-556cc8891a2c › d6dde138-847e-4418-aa6f-8155eae8115e
Learn Challenge: String Length | Strings
12345 string = "Practice is the ... in the string, including spaces. In the example, "Practice is the key to success" has 30 characters, so len(text) returns 30....
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python string length with examples
Python String length with Examples - Spark By {Examples}
May 31, 2024 - In this article, we will discuss several ways to return the length of the given string in Python. A string is a set of characters similar to C, C++, and
🌐
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.
Find elsewhere
🌐
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”.
🌐
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 - In the first output, we displayed the length of the entire color_list, which contained four string values, and the outcome was 4. In the second output, we printed the length of the first element in color_list, which is “Red”; it comprises 3 characters; therefore, the result was displayed as 3. Similar to the previous example, we have discussed the length function in a list; here, we have declared the tuple, which is another data structure in Python where we have displayed string values inside a tuple and printed the length of the whole tuple and also the size of the 2nd value inside the tuple.
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 - With Python Strings, to find the length of a string, one of the most used method is len () function. With len () function, we can measure the length of the string.
🌐
AlgoCademy
algocademy.com › link
String Length in Python | AlgoCademy
The len() function in Python returns the number of characters in a string. This includes letters, numbers, spaces, and special characters. Knowing the length of a string is crucial for various operations, such as slicing, indexing, and looping through the string. Here is a simple example to illustrate the concept:
🌐
Guru99
guru99.com › home › python › python len() function: how to find length of the string
Python len() Function: How to find length of the string
July 10, 2026 - Because the number of elements is stored on the object rather than recalculated each time, len() helps you check size quickly and optimize the performance of your program. The Python len() function uses the following syntax: ... value: The given ...
🌐
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.
🌐
Flexiple
flexiple.com › python › python-string-length
Python String Length - Flexiple
March 18, 2024 - When applied to a string, len() ... be they alphabetic, numeric, symbols, or whitespace. example_string = "Hello World" print(len(example_string)) # Output: 11...
🌐
Tutorialspoint
tutorialspoint.com › python › string_len.htm
Python String len() Method
str = "this is string example....wow!!!"; print "Length of the string: ", len(str)
🌐
Altcademy
altcademy.com › blog › how-to-get-the-length-of-a-string-in-python
How to get the length of a string in Python - Altcademy.com
June 13, 2023 - In the context of strings, length refers to the number of characters in the string. For example, the length of the string Hello is 5 because it consists of 5 characters: H, e, l, l, and o.
🌐
StrataScratch
stratascratch.com › blog › how-to-determine-python-string-length
How to Determine Python String Length? - StrataScratch
August 1, 2024 - That’s why learning the fundamentals of Python string should be very important. So, let’s start with its fundamentals first. Python strings resist change like stubborn mules. Why? Because, once they were created, they stay put. Why does this matter to you? Because consistency becomes your best friend in your coding adventures. Immutability ensures changes don’t ripple unexpectedly. Let’s see one quick example...
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-find-a-string-length
Python Program to find a String Length
April 21, 2026 - Next, the built-in len() function is applied to the user-given string to find the character count or length. str1 = input("Please enter your own String : ") print("Total Length of a Given String = ", len(str1))
🌐
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.
🌐
Metaschool
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
December 6, 2024 - 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. Ensure your slice indices are within the string’s length.