-2

I am new to python and a part of this course says this:

The following line of code will print the first character in each of the firstname and surname variables. Python starts counting at 0, so "Tom" would be T=0, o=1 and m=2.

print("Your initials are:",firstname[0],surname[0])

The question I am taking is where you have to take three letters from the start of a variable using this method, I am stuck so far, can anyone correct me.

surname = input () #input :smith
print (surname[0][1][2])
halfer
  • 19,471
  • 17
  • 87
  • 173
cameron claridge
  • 13
  • 1
  • 1
  • 3

1 Answers1

3

You can't just put the indices [0][1][2] right after each other - that does something completely different (and requires multidimensional arrays). What you need to do it something like:

print(surname[0:3])

This will print out 3 characters from surname starting at 0 (that is to say the first 3 characters)

heroworkshop
  • 345
  • 2
  • 6