📚
String Length
Measuring Strings with len()
The
len()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()- ◆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.")
💡💡
works on more than strings — it also works on lists, tuples, and dictionaries. It's one of Python's most versatile built-in functions!len()
main.pySandbox
main.py
Ctrl + Enter