Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

string

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.

like image 212
user2240033 Avatar asked Sep 08 '26 14:09

user2240033


2 Answers

You can use this:

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

where

x = 'this string'
x.split() # ['this', 'string']
like image 97
eumiro Avatar answered Sep 11 '26 03:09

eumiro


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.

like image 24
alex Avatar answered Sep 11 '26 04:09

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!