- Syntax
- Example 1: Finding the Length of a String
- Example 2: Length of a Sentence
- Example 3: Length of an Empty String
- Example 4: Length of a Numeric String
- Example 5: Length with Special Characters
- Example 6: User Input
- Example 7: Length of a String with Spaces
- Example 8: Storing the Length
- Example 9: Using len() in an Expression
- Example 10: Checking the Type of the Result
- Common Mistakes
- Best Practices
- Key Points to Remember
The length of a string is the total number of characters it contains.
Python provides the built-in len() function to determine the length of a string.
The len() function counts every character, including letters, digits, spaces, punctuation marks, and special characters.
Knowing the length of a string is useful when validating user input, processing text, checking password lengths, and controlling loops.
Syntax #
len(string_name)
Components #
| Component | Description |
|---|---|
len() |
Built-in function that returns the number of characters. |
string_name |
The string whose length is to be calculated. |
Example 1: Finding the Length of a String #
main.py
language = "Python"
print(len(language))
Output #
6
Explanation #
"Python"contains six characters.- The
len()function counts all characters. - The returned value is
6.
Example 2: Length of a Sentence #
main.py
sentence = "Python is easy."
print(len(sentence))
Output #
15
Explanation #
- The string contains letters, spaces, and a period.
- Spaces and punctuation are also counted as characters.
- Therefore, the total length is
15.
Example 3: Length of an Empty String #
main.py
text = ""
print(len(text))
Output #
0
Explanation #
- An empty string contains no characters.
- Therefore, its length is
0.
Example 4: Length of a Numeric String #
main.py
number = "123456789"
print(len(number))
Output #
9
Explanation #
- Although the string contains digits, each digit is treated as a character.
- There are nine characters in the string.
Example 5: Length with Special Characters #
main.py
password = "@Python#2026"
print(len(password))
Output #
12
Explanation #
- Letters, digits, and special symbols are all counted.
- Every character contributes to the total length.
- The length of the string is
12.
Example 6: User Input #
main.py
name = input("Enter your name: ")
print(len(name))
Sample Input #
Alexander
Output #
9
Explanation #
- The user enters a string.
len()counts the number of characters entered."Alexander"contains nine characters.
Example 7: Length of a String with Spaces #
main.py
message = "Good Morning"
print(len(message))
Output #
12
Explanation #
- The space between the two words is counted.
"Good"contains four letters."Morning"contains seven letters.- Including the space, the total length becomes
12.
Example 8: Storing the Length #
main.py
text = "Programming"
length = len(text)
print(length)
Output #
11
Explanation #
len(text)returns the length of the string.- The result is stored in the variable
length. - Printing
lengthdisplays11.
Example 9: Using len() in an Expression #
main.py
word = "Python"
print(len(word) + 4)
Output #
10
Explanation #
len(word)returns6.- The integer
4is added to it. - The final result is
10.
Example 10: Checking the Type of the Result #
main.py
text = "Python"
length = len(text)
print(length)
print(type(length))
Output #
6
<class 'int'>
Explanation #
- The
len()function returns an integer. - Therefore, its data type is
int.
Common Mistakes #
1. Assuming Spaces Are Not Counted #
Incorrect
text = "Hello World"
print(len(text))
Incorrect Expectation #
10
Actual Output #
11
Reason
The space between "Hello" and "World" is also counted.
2. Using length() Instead of len() #
Incorrect
text = "Python"
print(length(text))
Output #
NameError: name 'length' is not defined
Reason
Python provides the len() function, not length().
Correct
print(len(text))
3. Assuming len() Returns a String #
Incorrect
text = "Python"
print(len(text) + "2")
Output #
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Reason
len() returns an integer, not a string.
Correct
print(str(len(text)) + "2")
or
print(len(text) + 2)
4. Calling len() Without an Argument #
Incorrect
print(len())
Output #
TypeError: len() takes exactly one argument (0 given)
Reason
The len() function requires one argument.
Best Practices #
- Use
len()whenever you need the number of characters in a string. - Remember that spaces and punctuation are included in the count.
- Store the result in a variable if it will be used multiple times.
- Use meaningful variable names such as
length,size, orcount. - Remember that
len()returns an integer.
Key Points to Remember #
len()returns the number of characters in a string.- Every character is counted, including spaces and special symbols.
- The length of an empty string is
0. len()returns a value of typeint.len()requires exactly one argument.len()does not modify the original string.- It is one of the most commonly used built-in functions in Python.