Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a string with spaces?

Tags:

python

string

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

like image 247
misguided Avatar asked Jun 19 '13 05:06

misguided


People also ask

How do you repeat a string with spaces in Python?

To repeat a string in Python, We use the asterisk operator ” * ” The asterisk. is used to repeat a string n (number) of times. Which is given by the integer value ” n ” and creates a new string value.

How do you fill a string with spaces?

Use the String. format() method to pad the string with spaces on left and right, and then replace these spaces with the given character using String. replace() method. For left padding, the syntax to use the String.

How do you repeat a string?

JavaScript String repeat() The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.

How do you make a string repeating characters?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);


2 Answers

string = 'Hello There' print ' '.join([string[:5]] * 2) 
like image 176
neuront Avatar answered Sep 21 '22 04:09

neuront


In case if you want just to repeat any string

"Hello world " * 2  
like image 40
Vasili Pascal Avatar answered Sep 22 '22 04:09

Vasili Pascal