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
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.
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.
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.
Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);
string = 'Hello There' print ' '.join([string[:5]] * 2)
In case if you want just to repeat any string
"Hello world " * 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With