Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Find String Length in Python

  • Vijaykrishna RamVijaykrishna Ram
  • January 29, 2020
  • Built-in Methods, Python String
Find String Length in Python

We can find string length in Python using the built-in len() function. Let’s look at how this function works, and also let’s try finding the length of various types of Python strings using len().


Using len()

Let’s look at some simple examples to illustrate len().

>>> a = "Hello from AskPython"
>>> print(len(a))
20

This prints 20 because it is the number of characters in the string. We can thus find the length using len().

Even when the string has special characters, as long as it can be encoded in some Unicode format, we can calculate it’s length.

>>> a = 'AåBç'
>>> print(len(a))
4

For strings with special escape characters (they are prefixed by a backslash(\), only the character is counted towards the length and not the backslash. Examples include (\n, \t, \', etc)

>>> a = 'A\t\t'
>>> print(len(a))
3

>>> b = 'A\n\nB'
>>> print(len(b))
4

>>> c = 'A\'B'
>>> print(len(c))
3

For raw strings, since they treat backslash (\) as literal, the backslash will be counted towards the length of the string.

>>> s = r'A\t\t'
>>> print(len(s))
5

Working of len()

When we call the len() function using the String object, the __len__() method of the String object is called.

>> a = "Hello from AskPython"
>>> a.__len__()
20

To prove this, let us implement our own len() on a custom Class. Since __len__() works on objects, we must inherit the class object.

class Student(object):
    def __init__(self, name):
        self.name = name

    def __len__(self):
        print("Invoking the __len__() method on the Student Object to find len()...")
        count = 0
        for i in self.name:
            count += 1
        return count


a = Student("Amit")
print(len(a))

Since the len() method invokes __len__(), we will go through the function, which counts the number of objects in the iterable. Since we pass a string, we will simply get the length, which will come out to be 4!

Output

Invoking the __len__() method on the Student Object to find len()...
4

We have thus implemented our own len() method for the class Student! Amazing, isn’t it?


References

  • JournalDev article on String Length

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python String find()
Previous Post Python String find()
Next Post Remove Duplicate Elements from List in Python
Remove Duplicate Elements from List in Python

Related Posts

What Does %s Mean in Python String Formatting?

What Does %s Mean in a Python Format String?

  • November 30, 2026
Python Convert String to List

Python Convert String to List: Choose the Right Method

  • November 29, 2026
Translate String Python

Translate String Python

  • November 29, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.