'How can len() function count the number of letters in a word entered by the user?
As the user inputs a different word the length of words will change so I am trying to store the .len() answer in a variable which is not working.
This is what I tried:
letter=input("TYPE A WORD--> ")
letter.upper()
NO=letter.len()
print (NO)
Though there is an error message saying:
Traceback (most recent call last):
File "main.py", line 3, in \<module\>
NO=letter.len()
AttributeError: 'str' object has no attribute 'len'
Solution 1:[1]
There is no string.len() function in python. If you want to find the length of a string, you should use len(string) function. For example
NO = len(letter)
Solution 2:[2]
give letter as argument to len()
NO = len(letter)
Solution 3:[3]
Updated code for you
letter=input("TYPE A WORD--> ")
letter=letter.upper()
NO=len(letter)
print (NO)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniyal Ishfaq |
| Solution 2 | Nour SIDAOUI |
| Solution 3 | Ali Zahid Raja |
