Home » Python

Find length of the string using len() in Python

Python len() function with Example: In this article, we will learn about ‘len()’ function with an example. Example will print the length of the strings.
Submitted by IncludeHelp, on January 18, 2018

Python - len() function

len() function is an in-built function in Python which is used to find the length of the given string, it takes string as an argument and returns its length.

Note: In this program, we are using len() to get the length of the given string, but len() can also be used for other purpose like find the total number of elements in a list etc.

Syntax:

len(string)

Example:

    Input: "Hello"
    Output: 5

    Input: "Hello world"
    Output: 11

Python - source code

#defining two strings
str1 = "Hello"
str2 = "Hello World"

#printing length of the strings
print "Length of str1: ", len(str1)
print "Length of str2: ", len(str2)

#printing length of the direct string value
print "Length of \"IncludeHelp\": ", len("IncludeHelp")

Output

$python main.py
Length of str1:  5
Length of str2:  11
Length of "IncludeHelp":  11

Printing lengths without using messages

#defining two strings
str1 = "Hello"
str2 = "Hello World"

#printing length of the strings
print(len(str1))
print(len(str2))

#printing length of the direct string value
print(len("IncludeHelp"))

Output

$python main.py
5
11
11



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.