Python 程序打印元组项

编写一个 Python 程序来打印元组中的所有项目。我们可以使用 print 函数来打印整个元组。在此示例中,我们声明了字符串和整数元组并打印它们。

numTuple = (10, 20, 30, 40, 50)

print("The Tuple Items are ")
print(numTuple )

strTuple = ('C#', 'Java', 'Python', 'C')

print("\nThe String Tuple Items are ")
print(strTuple ) 
Python Program to Print Tuple Items 1

在此程序中,我们使用 for 循环范围来访问每个元组项。第一个 for 循环从第一个元组项迭代到最后一个并打印每个元素。第二个循环打印字符串元组中的所有水果。

numTuple = (10, 20, 30, 40, 50)

print("The Tuple Items are ")
for i in range(len(numTuple)):
print("Tuple Item at %d Position = %d" %(i, numTuple[i]))

print("=========")
fruitsTuple = ('apple', 'orange', 'kiwi', 'grape')
for fruit in fruitsTuple:
print(fruit)
The Tuple Items are 
Tuple Item at 0 Position = 10
Tuple Item at 1 Position = 20
Tuple Item at 2 Position = 30
Tuple Item at 3 Position = 40
Tuple Item at 4 Position = 50
=========
apple
orange
kiwi
grape