Python - String concatenation and Repetition

Introduction

You can concatenate strings using the + operator and repeat them using the * operator:

Demo

print( len('abc') )            # Length: number of items 
print( 'abc' + 'def' )         # Concatenation: a new string 
print( 'Ni!' * 4 )             # Repetition: like "Ni!" + "Ni!" + ...
# w  w  w. j a va  2s . c o  m

Result

The len built-in function returns the length of a string.

Adding two string objects with + creates a new string object, with the contents of its operands joined.

Repetition with * is like adding a string to itself a number of times.

To print a line of 80 dashes, you can count up to 80, or let Python count for you:

Demo

print('-' * 80) # 80 dashes, the easy way

Result