Length of String
Measure a string with len, including spaces and punctuation. Beginners trace text input and distinguish Unicode code-point length from bytes or visible symbols.
1# Take string input2s = input("Enter a string: ")34# Get length5length = len(s)6print(f"Length: {length}")
Output
Enter a string: Hello Length: 5
In this one
- Measure code-point length
- Include whitespace
- Distinguish text from bytes
How it works
- 1
Read s with input.
- 2
Calculate len(s).
- 3
Print the returned character-position count.
Edge cases
What is the length of empty input?
What is the length of empty input?
Zero.
Does a space count?
Does a space count?
Yes. Each ordinary space is a character in the string.
What's going on
input returns the entered text without its terminating newline. len counts the string's Unicode code points, not its storage bytes.
A space is part of the text and contributes to length. Some visible symbols use multiple code points, so a displayed symbol need not count as one. The code neither trims spaces nor splits words before measuring.
What is the length of empty input? Zero.
Does a space count? Yes. Each ordinary space is a character in the string.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Does len count words here?
Pick an answer to check it.
Question 2
What is the length of empty input?
Pick an answer to check it.
Questions people ask
What is the length of empty input?
Zero.
Does a space count?
Yes. Each ordinary space is a character in the string.
Does len count words here?
No, it counts code points.
๐Related Content
- ๐
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- ๐
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- ๐ป
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum