Home > OS >  I can't understand the problem in this python code
I can't understand the problem in this python code

Time:09-20

name = input("What is your name?")
len(name)
print(name   " has "   len   " number of letters.")

CodePudding user response:

either

l=len(name)
print(name   " has "   str(l)   " number of letters.")

or

print(name   " has "   str(len(name))   " number of letters.")

this should solve the problem

CodePudding user response:

best way to do this is using comma instead of spaces in the string and changing the int to string.

print(name,"has",len(name),"number of letters.")

CodePudding user response:

Firstly, len is a function, so it is not correct syntax to try to string manipulate this.

A working answer could be to save the length to a variable and then use that variable in your print statement, (like described in the comments above). I would also make this an f-string for more easy use with the variables:

name = input("What is your name?")
name_length = len(name)
print(f"{name} has {name_length} number of letters.")
  • Related