How to repeat a string with spaces?
Question:
I am trying string repetition in 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
Answers:
string = 'Hello There'
print ' '.join([string[:5]] * 2)
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 str
as a variable because its a keyword in python.
Because then you’re getting the space in between the two words and putting it in between the hello’s
that should work!
P.S you don’t need the #!/bin/python
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.
import re
str = 'Hello There'
m = re.match("(w+ )",str)
m.group(1) * 2
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.join
as in nuront’s answer is probably better.
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 repr
of 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.
In case if you want just to repeat any string
"Hello world " * 2
I am trying string repetition in 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
string = 'Hello There'
print ' '.join([string[:5]] * 2)
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 str
as a variable because its a keyword in python.
Because then you’re getting the space in between the two words and putting it in between the hello’s
that should work!
P.S you don’t need the #!/bin/python
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.
import re
str = 'Hello There'
m = re.match("(w+ )",str)
m.group(1) * 2
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.join
as in nuront’s answer is probably better.
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 repr
of 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.
In case if you want just to repeat any string
"Hello world " * 2