Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

BeginnerPhase 5 - Category 1: Basic String HandlingExample 1 of 10
length-of-string.py
1# Take string input
2s = input("Enter a string: ")
3
4# Get length
5length = 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. 1

    Read s with input.

  2. 2

    Calculate len(s).

  3. 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.