Python Forum

Full Version: Homework Teacher isnt helping need some help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

This is wrong somewhere on the last 2 lines please help

# TODO: author

oneTwoThree = "123"
print(len(oneTwoThree))

montyPython = "Monty Python"
print(len(montyPython))

word = input("Please enter a word:123 ")
print("The length of " + word + " is " + len(3))
The 'len' function takes a string as an input and returns the length of the string. In the last line, you used the number 3 as an input to len(), which doesn't work because 3 is not a string. 

If you want to find the length of the word entered by the user, write len(word)- word is the name of your variable
Quote:print("The length of " + word + " is " + len(3))
You should also know that no one in python concatenates strings together like that, as in other languages.
The pythonic way is to use format/ the first being f string python3.6+ and format() for anything older.
>>> word = 'monty python'
>>> f"The length of {word} is {len(word)}"
'The length of monty python is 12'
>>> "The length of {} is {}".format(word, len(word))
'The length of monty python is 12'