📚

String Length

Measuring Strings with len()

The

len()
function counts how many characters are in a string — including spaces, numbers, and special characters:

print(len("Python"))       # → 6
print(len("Hello World"))  # → 11  (space counts!)
print(len(""))             # → 0   (empty string)
print(len("Hi!\n"))       # → 4   (\n counts as ONE character)

len()
is useful for:

  • Validating input length (passwords, usernames)
  • Looping through characters
  • Checking if a string is empty
password = input("Create a password: ")
if len(password) < 8:
    print("Too short! Use at least 8 characters.")
💡

💡

len()
works on more than strings — it also works on lists, tuples, and dictionaries. It's one of Python's most versatile built-in functions!

main.pySandbox
main.py