开发者

How do I put string at the end of every line? [closed]

开发者 https://www.devze.com 2023-03-30 16:45 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消