Search code examples
pythonstring-concatenation

Concatenate same string x times


There's a shortand way to concatenate a string x times instead of using something like this?

z = 'val'
y = ''
for x in range(1,10):
    y += z

Solution

  • In python you can just multiply the string to do this.

    >>> mystr = 'nick'
    >>> mystr*10
    'nicknicknicknicknicknicknicknicknicknick'