1

I currently have this for assignment:

word = input("Please enter a word: ")

print("The length of " + word + " is " + len(word = int(word)))

It runs and I enter Lilith Qua

I run into an error said that:

ValueError: invalid literal for int() with base 10: 'Lilith Qua'

Is there away to fix this?

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Lth
  • 33
  • 1
  • 3
  • What do you expect `len(word = int(word))` to do? `, len(word)` is sufficient. – AChampion May 11 '17 at 02:48
  • 1
    By the way it's better practice to use [string formatting](https://docs.python.org/2/library/string.html#format-string-syntax) rather than concatenation. `print("The length of {} is {}".format(word, len(word)))` it also takes care of conversion. – umutto May 11 '17 at 02:51

1 Answers1

3

You have to convert the int to string before you concatenate.

You have to use

print("The length of " + word + " is " + str(len(word)))

String formatting can also be used as,

print("The length of %s is %d"%(word,len(word)))

Here,

  • %s is for string

  • %d is for int

Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
  • print("The length of " + word + " is " + str(len(word))) is the right answer Thank you. You help me out. – Lth May 11 '17 at 05:50