Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialshariq ali
444 PointsMake a new variable named var_one that is either a list or a string. Make sure that var_one
index challenge i can't solve
var_one[5] = 'trust'
var_one.index('5')
1 Answer
Krishna Pratap Chouhan
15,203 PointsLets see... Task is: Make a new variable named var_one; Make sure that var_one[5] is the letter "t".
Since everything in python is an object, which means so is string. hence if there is an variable say, var_one="string", you can use the var_one as you use an array. That means, var_one[0]='s'; var_one[1]='t';... and so on.
Now the second task is to make sure that var_one[5] is 't'. Now remember that indexing starts from 0(zero) so there must be atleast 6 letters in the variable.
hence in your code:
var_one = 'trust'
print(var_one[4])
will print 't'. But var_one[5] does not exists. so ...
var_one = 'trustt'
here var_one[5] exists and is 't'.
Another point to remember, in Python there is reverse indexing that means, there are index starting from -1 as well. In above example, var_one[-1]='t', var_one[-2]='t', var_one[-3]='s', and so on.
Hope this helps!