Python string length | len() function and Other ways

The best way to get Python string length is by using len() function. len() function is an inbuilt function in Python programming language.

You can use other ways also to find the length of Given a string. Here are the ways to find the length of a string in python without using len() function in Python.

Syntax

Return Value

It’s returns the length(in number) of the string.

String len() python example

The following example shows the usage of len() method to get the string size.

Output: 12

Python string length 0

If String is of length zero that means it is an empty string. If you try to print this string, then it will print nothing.

Output: You can see the empty space of string in console.

Python string length len function

Q: How to find the size of the string in bytes in Python?

Answer: If you want the number of bytes in a string, this code should do work for you.

The output will be number because encapsulated in a string is a bunch of other information due to the fact that strings are actual objects in python.

Output: 5

Q: How to find the length of the string in python without using len method?

Answer: You can use loop and in operator and other method to get size of string.

A string will be iterated over, directly in a for-loop and count the number of iterations. It will be result of the length the string.

Output: 8

Do comment if you have any doubts and questions on this tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python sets Examples are in Python 3, so it may change its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.