Python - List Concatenation and Repetition

Introduction

Lists are sequences, therefore lists support many of the same operations as strings.

For example, lists respond to the + and * operators much like strings.

They mean concatenation and repetition here too:

Demo

print( len([1, 2, 3]) )                          # Length 
print( [1, 2, 3] + [4, 5, 6] )                   # Concatenation 
print( ['Ni!'] * 4 )                             # Repetition
# from ww  w.jav  a2s .  c o m

Result

You cannot concatenate a list and a string.

You have to first convert the list to a string using str or % formatting or convert the string to a list via the list built-in function:

Demo

print( str([1, 2]) + "34" )                       # Same as "[1, 2]" + "34" 
print( [1, 2] + list("34") )                      # Same as [1, 2] + ["3", "4"]

Result