Get the length of a Python string
String length
len
function returns the number of characters contained
in a string.
print len('abc') # length: number items
The code above generates the following result.
Get the length of a string with escape characters inside
x = "C:\py\code" # keeps \ literally
print x# from ww w .j a va 2 s.c o m
print len(x)
s = 'a\0b\0c'
print s
print len(s)
The code above generates the following result.
Output and get length of a string with special characters
s = '\001\002\x03'
print s
print len(s)
The code above generates the following result.
range() with len() for indexing into a string
foo = 'abc'
for i in range(len(foo)):
print foo[i], '(%d)' % i
The code above generates the following result.