2

So say I have

x = "this string"

I'm trying to print the two words on different lines. I'm thinking that I need to use string.split() in some way but I'm not sure how.

4

3 回答 3

8

你可以使用这个:

print '\n'.join(x.split())

在哪里

x = 'this string'
x.split() # ['this', 'string']
于 2013-04-03T10:45:12.250 回答
2

您不必使用split(). 这应该这样做。

print x.replace(' ', '\n')

键盘

它用换行符替换空格字符。

于 2013-04-03T10:46:47.803 回答
2
x="this string"
for a in x.split():
    print a
于 2013-04-03T10:49:16.827 回答