Inspect the code carefully and write a prediction in the comments about what it will do.
- Run the code and compare the result to their prediction.
Learning Objectives/Goals
Be able to read, comprehend, trace, adapt and create Python code using selection that:
- Uses len to count the number of characters in a string
The **len() **function returns the number of characters in a string. It takes the string as its parameter.
This code gets input, stores it in the word variable and finds out how many characters are in it.
print("Enter a word")
word = input()
Uses the len() function as part of the print() function. Removes the need for the word_length variable.
print(len(word))
Help! My Code Does Not Work!
Make sure that you check for the following things:
- len is lower case
- the length of the string has been assigned into a variable
- spaces are characters too!