Python Introducing Tuples Getting to Know Tuples Creating Tuples

Malcolm Aldrin Masumba
Malcolm Aldrin Masumba
1,282 Points

How do i assign a variable to an element in a tuple

Where exactly am i going wrong

1 Answer

Chris Kisselburgh
Chris Kisselburgh
2,172 Points

Hey Malcolm,

Remember that tuples are immutable meaning they can't be modified once the data is stored inside of one. With that in mind, you access items in tuples using their position index inside of square brackets.

my_tuple = ('dog', 'cat', 'bird')
print(my_tuple[1])

This code should return the string 'cat' as the position index in python always starts at the number 0. Instead of printing the variable like I did in my example, you could just assign it to a new variable as well.

Hope this helps!