FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Another Python Question .replace()





davidv
Hi,

I have a multiline string that's not created on a windows machine and thus not having the \r\n\r\n and I want to replace every first orruance in each line with another character. For example,
Code:
s = 'Hello says hi
Goodbye says bye
Greeting also says hi'


and I want it to replace the first space of each line with a colon
Code:
>>> print s
>>> Hello:says hi
>>> Goodbye:says bye
>>> Greeting:also says hi


So far I've made it only replaces the first occurance s.replace(' ', ':',1).
Code:
>>> print s
>>> Hello:says hi
>>> Goodbye says bye
>>> Greeting also says hi

Can anyone help?
Peterssidan
try this
Code:
'\n'.join([x.replace(' ', ':',1) for x in s.splitlines()])
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.