all Python 3Strings (str)term
len() for strings
length
Returns the number of characters in a string. Every Unicode character counts as one.
Syntax
len(s)
len counts code points, not what the eye reads as one character: an emoji with a skin-tone modifier or a letter written with a separate combining accent comes out as 2 or more. Encode the same text to UTF-8 and the length changes — Cyrillic takes two bytes per letter there — so never mix up the length of a string with the length of its bytes. The call itself is O(1); the length is stored on the object.
Examples
print(len(''))
# → 0
print(len('hello'))
# → 5
print(len(' '))
# → 3
print(len('Привет'))
# → 6
s = 'test'
if len(s) > 3:
print('длинная')
# → 'длинная'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy