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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
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 ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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....
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
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").
๐ŸŒ
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))
๐ŸŒ
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.
๐ŸŒ
Enterprise DNA
blog.enterprisedna.co โ€บ python-length-of-string
Python: Length of String, Letโ€™s Find It! โ€“ Master Data Skills + AI
The easiest and most effective way to find the length of a string in Python is to use the built-in len() function. By using len(), developers can quickly retrieve the length of not only strings but also other data types like lists, tuples, and dictionaries.