Python len – How to use string length method

Python len – How to use string length method

The string length method in Python

The Python len() method returns the total number of characters in a given string. The string length method is quite useful especially if your program allows the limited number of characters for a text field or some other case.

The string length method is quite useful especially if your program allows the limited number of characters for a text field or some other case.

An example of string length method

Syntax of Python length method

Following is the basic syntax of the len() method:

len(string_name)

A string length example

The example below shows how the len() method of Python is used to return the total number of characters in the given string. For that, we have created a string variable and assigned it a value at the time of declaration.

After that, we used the print function to display the output. In the print function, we used the len() method to return the total characters in that string variable.

Python string length

See this example online

The Output:

The length of string is: 28

 

Further Reading: Python String