给予这个元组(包括数字):
tup1 = ('physics', 'chemistry', 1997, 2000)
我希望将每个索引作为字符串并检查它是否为空或空格
for x in tup1:
if(x is not ' '):
print(x)
我尝试使用str(x)
,' '.join(x)
,xx=print(x)
但它们都不起作用。
答案 0 :(得分:0)
tup1 = ('physics', 'chemistry', 1997, 2000, ' ', '');
result = [str(x) for x in tup1
if x != '' and x != ' ']
print(result) # ['physics', 'chemistry', '1997', '2000']