Python Coding Exercise #30: Number of Characters
Part of the series Python Practice Exercise — part 30
Before you start: Know input() and if statements · Know what a string is
The question
The function
lencan be used to find out the length of a string, among other things. The function returns the number of characters in a string.
Some examples of how this works:
word = "abcd"
print(len(word))
print(len("hi there"))
word2 = "howdydoody"
length = len(word2)
print(length)
empty_string = ""
length = len(empty_string)
print(length)
4
8
10
0
Please write a program which asks the user for a word and then prints out the number of characters, if there was more than one typed in.
Examples of expected behaviour:
Please type in a word: hey
There are 3 letters in the word hey
Thank you!
Please type in a word: banana
There are 6 letters in the word banana
Thank you!
Please type in a word: b
Thank you!
My Solution
word = input("Please type in a word: ")
length = len(word)
if length <= 1:
print("Thank you!")
else:
print(f"There are {length} letters in the word {word}\nThank you!")
How My Solution Works
1. Read the word and measure it
input() returns the word as a string, and len(word) counts its characters. For hey that’s 3, and for banana it’s 6. The count is stored in a variable called length, so it can be used twice without calling len() again.
2. Two cases
The exercise says the count should be printed only “if there was more than one typed in”. So there are two cases, and each has its own print():
- One character or none (
length <= 1): onlyThank you!is printed. - More than one character (anything else): the program prints the count and the word, then
Thank you!.
The \n inside the f-string starts a new line, so one print() outputs both lines. (More on \n in Data Types in Python.)
3. Getting the boundary right
This exercise is a small lesson in comparisons. “More than one” means greater than 1, so a single character does not count. Checking the boundary values shows whether the condition is right:
| Word | length | Is it more than 1? | Prints the count? |
|---|---|---|---|
| (empty, just Enter) | 0 | no | no |
b | 1 | no | no |
ab | 2 | yes | yes |
hey | 3 | yes | yes |
The condition length <= 1 covers the first two rows, and the else covers the rest. If you wrote length < 1 by mistake, the word b would wrongly get the count. (More about choosing between < and <= in Exercise #20.)
University Solution
The University Solution turns the condition around, and moves Thank you! outside the if:
word = input("Please type in a word: ")
length = len(word)
if length > 1:
print("There are", length, "letters in the word " + word)
print("Thank you!")
Two differences:
- There is no
else. Theifonly handles the special case: when the word has more than one character, the count is printed. Otherwise, theifis skipped. Thank you!is printed once, outside theif. It’s not indented, so it runs in every case. In My Solution,Thank you!is written in both branches. This is the same idea as in Exercise #14: if a line should always run, write it once, after theif.
The output is exactly the same. The University Solution’s print line also mixes two styles: commas and +.
print("There are", length, "letters in the word " + word)
The commas put a space between the values, and they let print() accept the number length without converting it. The + word joins two strings. That’s why "letters in the word " has a space at the end of the quotes, since + adds nothing on its own. (Commas and spaces in print() were the subject of Exercise #05.)
Things to notice
len() counts every character, not only letters. Spaces, digits and punctuation all count:
print(len("hi there")) # 8, because the space counts
print(len("12345")) # 5
print(len("Hello, world!")) # 13
So typing hi there prints There are 8 letters in the word hi there. The message says “letters”, but the program really counts characters.
An empty answer has length 0. If the user just presses Enter, word is an empty string, len("") is 0, and the program prints only Thank you!.
len() works on more than strings. It also gives the number of items in a list, a tuple or a dictionary:
print(len([1, 2, 3])) # 3
print(len((1, 2))) # 2
print(len({"a": 1})) # 1
But it doesn’t work on a plain number. len(12345) stops with TypeError: object of type 'int' has no len(). If you want the number of digits, convert it to a string first: len(str(12345)) is 5.
Key takeaways
len(x)returns the number of characters in a string. It also counts the items in a list, tuple or dictionary.- Spaces, digits and punctuation all count as characters.
- The empty string
""has length 0. - “More than one” means
> 1. Check the boundary values 0, 1 and 2 to be sure. - A line that should run in every case belongs outside the
if, written once. len()doesn’t work on a plain number. Convert it withstr()first.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.