len() Function

The len() function returns the length of a string expression.

Syntax

len(str.exp)

Parameter(s)

str.exp

Expression whose length is returned.

Example(s)

This assignment statement uses the len() function to delete the last character of string.

customer = "john"

name.length = len(customer)

print name.length

4

string = "test"

string = string[1,len(string)-1]

print string

tes

This outputs a 5.

string = ’abcde’

print len(string)

This code gets the length of the result of an arithmetic expression. The result of the arithmetic expression is converted to an ASCII string when the len() function is invoked. The output is 2, because 5 times 10 is 50, and the length of 50 is 2 characters.

a = 5

x = len(a * 10)

print x

2

See Also

BASIC Functions

Statements and Functions