【发布时间】:2017-04-13 21:15:56
【问题描述】:
我有一个变量,它可以是单个字符串或字符串列表。当变量为单个字符串时,循环按字符对其进行迭代。
text = function() # the function method can return a single string or list of string
for word in text:
print word+"/n"
# When it is a single string , it prints char by char.
当它是一个字符串时,我希望循环只迭代一次。我实际上不想使用其他循环类型。我怎样才能为每个结构做到这一点?
【问题讨论】:
-
检查
text的类型怎么样? -
在单个字符串的情况下,您可以将 [yourstring] 作为由单个元素组成的列表返回。
标签: python string python-2.7 for-loop