user2240033
user2240033

Reputation: 71

Python: How do I print a string that contains multiple words on different lines?

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.

Upvotes: 3

Views: 2102

Answers (3)

namit
namit

Reputation: 6957

x="this string"
for a in x.split():
    print a

Upvotes: 2

alex
alex

Reputation: 490143

You don't have to use split(). This should do it.

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

CodePad.

It replaces the space character with the newline character.

Upvotes: 2

eumiro
eumiro

Reputation: 212825

You can use this:

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

where

x = 'this string'
x.split() # ['this', 'string']

Upvotes: 8

Related Questions