How to Repeat Strings in Python
Repeating strings is a common task in programming.
This guide explores various techniques to repeat strings in Python, including using the multiplication operator, repeating substrings, controlling the length of the repeated string, adding separators, and repeating individual characters.
Repeating Strings with the Multiplication Operator
The most direct way to repeat a string is by using the multiplication operator (*
) with an integer.
my_str = 'David'
new_str = my_str * 2
print(new_str) # Output: DavidDavid
- The multiplication operator repeats the string the specified number of times and concatenates the resulting strings together.
1. Repeating Substrings
Use string slicing to repeat only a portion of the string:
my_str = 'David'
new_str = my_str[0:3] * 2
print(new_str) # Output: DavDav
- The slice
my_str[0:3]
extracts the first three characters, which are repeated using the multiplication operator.
2. Repeating Integers as Strings
To repeat an integer multiple times, you must convert it to a string first using str()
:
my_int = 9
print(str(my_int) * 4) # Output: 9999
3. Repeating Strings with f-strings
You can also embed string repetition within f-strings:
my_str = 'z'
result = f'Result: {my_str * 4}'
print(result) # Output: Result: zzzz
Repeating Strings to a Certain Length
To repeat a string to match a certain length, you can multiply and slice the string.
1. Repeating Strings to a Certain Length by Multiplying and Slicing
Multiply the string by a number greater than the desired length, and then slice the result:
def repeat_to_length(string, length):
return (string * (length//len(string) + 1))[:length]
print(repeat_to_length('asd', 6)) # Output: asdasd
print(repeat_to_length('asd', 4)) # Output: asda
- The
length//len(string)
computes how many repetitions of the string you would have to have if the string had a length equal to the length parameter. Adding one will make sure that the multiplication will make the string long enough. - The slice
[:length]
then extracts exactlylength
characters from the resulting string.
2. Repeating strings to a certain length by multiplying and slicing (a simpler approach)
A more direct, simpler approach is to multiply and slice:
def repeat_to_length_2(string, length):
return (string * length)[:length]
print(repeat_to_length_2('asd', 6)) # Output: asdasd
print(repeat_to_length_2('asd', 4)) # Output: asda
- While slightly less optimized this approach is simpler and easy to understand.
Repeating Strings with a Separator
To repeat a string with a separator, use str.join()
:
my_str = 'David'
new_str = ' '.join([my_str] * 2)
print(new_str) # Output: David David
new_str = '-'.join([my_str] * 2)
print(new_str) # Output: David-David
- The multiplication operator creates a list of the string repeated N times:
['David', 'David']
. - The
str.join()
method then concatenates the strings in the list using the specified separator.
Repeating Each Character in a String
To repeat each individual character in a string, use a generator expression, or the map
function.
1. Using Generator Expressions
The most direct way to do this is with a generator expression:
my_str = 'asd'
N = 2
new_str = ''.join(char * N for char in my_str)
print(new_str) # Output: aassdd
- The generator expression
(char * N for char in my_str)
iterates over each character, and repeats it N times. - The
str.join()
joins the generated results into a single string.
2. Using map()
with Lambda Functions
An alternative approach is to use the map()
function:
my_str = 'asd'
N = 3
new_str = ''.join(map(lambda char: char * N, my_str))
print(new_str) # Output: aaasssddd
- The
map()
function applies the lambda function to each character in the string. - The
lambda
function repeats the current character N times. - The
str.join()
method is used to join the strings returned bymap()
.