How do I put string at the end of every line? [closed]
I am wondering how could I add " Hello " to the end of every line?
E开发者_运维知识库xample:
Super
Cool
Fun
Output:
Super Hello
Cool Hello
Fun Hello
lines = "Foo\nBar\nBaz"
for line in lines.splitlines():
print "%s Hello" % line
You could just use the string function replace. It would look like:
In [3]: a = 'Super\n Cool\n Fun\n'
In [4]: a.replace('\n', ' Hello \n')
Out[4]: 'Super Hello \n Cool Hello \n Fun Hello \n'
['%s hello' % s for s in ['super', 'cool', 'fun']]
If your data is in a list.
精彩评论