转换元组thant包括数字和字符串到python3中的字符串

时间:2017-12-24 11:06:44

标签: python

给予这个元组(包括数字):

tup1 = ('physics', 'chemistry', 1997, 2000)

我希望将每个索引作为字符串并检查它是否为空或空格

for x in tup1:
  if(x is not ' '):
    print(x) 

我尝试使用str(x)' '.join(x)xx=print(x) 但它们都不起作用。

1 个答案:

答案 0 :(得分:0)

tup1 = ('physics', 'chemistry', 1997, 2000, ' ', '');

result = [str(x) for x in tup1
          if x != '' and x != ' ']

print(result)  # ['physics', 'chemistry', '1997', '2000']