🌐
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 ...
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
Get the character at position 1 ... about For Loops in our Python For Loops chapter. To get the length of a string, use the len() function....
🌐
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)
🌐
Cach3
w3schools.com.cach3.com › python › gloss_python_string_length.asp.html
Python String Length - W3Schools
Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative Indexing on a String Check In String Format String Escape Characters ... Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Filter List Sort List · HTML CSS JavaScript SQL Python PHP jQuery Bootstrap XML Read More » ... If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: ... Your message has been sent to W3Schools.
🌐
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. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or ...
🌐
Tutorialspoint
tutorialspoint.com › python › string_len.htm
Python String len() Method
For example, the string "Tutorials Point" has 15 characters in it including the space. Following is the syntax of Python String len() method: ... This method does not accept any parameter. This method returns the length of the string.
🌐
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 - The Python len() function uses the following syntax: ... value: The given object — such as a string, list, tuple, dictionary, or set — whose length you want to measure.
🌐
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
🌐
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....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-length-len
Python string length - GeeksforGeeks
October 15, 2024 - 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. In this example, we are finding the length of a string.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-1.php
Python: Calculate the length of a string - w3resource
June 12, 2025 - # Define a function named string_length that takes one argument, str1. def string_length(str1): # Initialize a variable called count to 0 to keep track of the string's length. count = 0 # Iterate through each character in the input string str1. for char in str1: # For each character encountered, increment the count by 1. count += 1 # Return the final count, which represents the length of the input string. return count # Call the string_length function with the argument 'w3resource.com' and print the result. print(string_length('w3resource.com')) ... Write a Python program to calculate the length of a string recursively without using the built-in len() function.
🌐
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 ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Metaschool
metaschool.so › home › answers › how to use python string length?
How to Use Python String Length?
December 6, 2024 - To find out string length in Python, you can use the len() function. This built-in function returns the number of characters in the string, including letters, numbers, spaces, and punctuation.
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
Strings in Python: Length, Indexing, Slicing, Finding & Replacing | Study.com
To determine string length in Python, we use the built-in len() function which returns the length of the string. For example:
🌐
Cisco
ipcisco.com › home › python string length
Python String Length | How to Calculate String Lenght in Python?⋆ IpCisco
December 27, 2021 - Here, we will define our own function. def myfunction(x): counter = 0 for i in x: counter += 1 return counter x = "how are you?" print(myfunction(x)) As you can see above, we have defined a function named myfunction.
🌐
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”.
🌐
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...
🌐
Enterprise DNA
blog.enterprisedna.co › python-length-of-string
Enterprise DNA: We Help Businesses Put Data and AI to Work
Learn data and AI skills on the platform, bring us in on a project, or run a managed Omni Command Centre. 220K+ professionals trained, 1,500+ companies impacted globally.
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
🌐
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.