Python重复字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17183259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:42:17  来源:igfitidea点击:

Python repeat string

python

提问by misguided

I am trying string repetition in Python.

我正在尝试在 Python 中重复字符串。

#!/bin/python
str = 'Hello There'
print str[:5]*2

Output

输出

HelloHello

你好你好

Required Output

所需输出

Hello Hello

你好你好

Can anyone please point me in the right direction .

任何人都可以指出我正确的方向。

Python version: 2.6.4

Python版本:2.6.4

采纳答案by neuront

string = 'Hello There'
print ' '.join([string[:5]] * 2)

回答by Serial

Do this:

做这个:

str = 'Hello There'
print str[:6]*2

that will ad a space after the second "Hello" if that's ok. Also, like rajpy said you shouldn't use stras a variable because its a keyword in python.

如果没问题,这将在第二个“你好”之后添加一个空格。另外,就像 rajpy 说你不应该str用作变量,因为它是 python 中的一个关键字。

Because then you're getting the space in between the two words and putting it in between the hello's

因为那样你就会在两个词之间留一个空格,然后把它放在 hello 之间

that should work!

那应该工作!

P.S you don't need the #!/bin/python

PS你不需要 #!/bin/python

回答by rajpy

Try this:

尝试这个:

print (str[:5] + ' ') * 2

If you want to specify trailing space explicitly.

如果要明确指定尾随空格。

In your example, you could do:

在您的示例中,您可以执行以下操作:

print str[:6] * 2

Please don't use built-in types(str, int etc..) as variables in your program, it shadows its actual meaning.

请不要在程序中使用内置类型(str、int 等)作为变量,它会掩盖其实际含义。

回答by Reegan Miranda

import re
str = 'Hello There'
m = re.match("(\w+\ )",str)
m.group(1) * 2

回答by Blckknght

Here's an alternative solution, using string formatting with a repeated index:

这是一个替代解决方案,使用带有重复索引的字符串格式:

print "{0} {0}".format(s[:5])   # prints "Hello Hello" if s is "Hello World"

This will work well if you know ahead of time exactly how you want to repeat your string. If you want the number of repetitions to variable at run time, using str.joinas in nuront's answer is probably better.

如果您提前确切地知道要如何重复您的字符串,这将很有效。如果您希望在运行时重复次数可变,str.join那么在 nuront 的答案中使用as 可能更好。

An advantage to using string formatting is that you're not limited just to repetition, though you can do it easily enough. You can also do other decorating in and around the the string, if you want (and the copies don't need to be treated the same):

使用字符串格式的一个优点是您不仅限于重复,尽管您可以很容易地做到这一点。如果需要,您还可以在字符串内部和周围进行其他装饰(并且副本不需要被相同对待):

 print "[{0!r}] ({0:_^15})".format(s[:5])   # prints "['Hello'] (_____Hello_____)"

That prints the reprof a first copy of a string inside of square brackets, followed by a second copy in parentheses, centered and padded by underscores to be 15 characters wide.

这会repr在方括号内打印字符串的第一个副本,然后在括号中打印第二个副本,居中并用下划线填充为 15 个字符宽。

回答by Vasili Pascal

In case if you want just to repeat any string

如果您只想重复任何字符串

"Hello world " * 2