James Walker
James Walker

Reputation: 35

Any way to access values in a tuple?

I was wondering if there was any way to access a value in a tuple?

for example:

a = [('ANTA01H3F', 'LEC02', '17-12-07', '14:00')]

and i wanted just '17-12-07' and '14:00'. Is there a way i can get these values and attach them to a string.

print("The date is" + '17-12-07' + " and time is" + '14:00')

Upvotes: 1

Views: 51

Answers (3)

furas
furas

Reputation: 142641

a = [('ANTA01H3F', 'LEC02', '17-12-07', '14:00')]

It is not tuple - it is list with one element - tuple

To get value you have to use two indexes

date = a[0][2]

This is tuple

a = ('ANTA01H3F', 'LEC02', '17-12-07', '14:00')

date = a[2]

Upvotes: 4

Sayan Sil
Sayan Sil

Reputation: 6095

You can access them as you would in a normal list, using indices. If tup=(1,2,3) then tup[1] is 2

Upvotes: 0

Reblochon Masque
Reblochon Masque

Reputation: 36662

You can unpack the tuple, then print using string format:

a = [('ANTA01H3F', 'LEC02', '17-12-07', '14:00')]
_, _, date, time = a[0]
print(f"The date is {date} and time is {time}")

Upvotes: 1

Related Questions