String repetition and concatenation


You might be used to the fact that you can only multiply numbers, but in python you can also "multiply" a string by a number. It is called repetition. In this example we have a string "Jar " that we repeat twice.

We can also add two strings to concatenate them together.

I don't think the repetition operator is used very often, but in one case it could come in very handy. When you are writing some text report and you'd like to add a long line of dashes that would be exactly the same length as your title.


examples/strings/repetition.py
name = 2 * 'Jar '
print(name)        # Jar Jar

full_name = name + 'Binks'
print(full_name)   # Jar Jar Binks


title = "We have some title"
print(title)
print('-' * len(title))

# We have some title
# ------------------