liveBook · Manning

String repetition operator

As explained in Learn AI-Assisted Python Programming, Second Edition

A string repetition operator is a language operation that produces a string by repeating another string a specified integer number of times.

The * operator repeats a string a specified number of times, producing a new string rather than performing numeric multiplication. section 4.3.4

Print a repeated separator

print('-=' * 5) produces -=-=-=-=-=, repeating the string five times. Compare this with +, which joins two strings together. section 4.3.4

>>> first = 'This is a '
>>> second = 'sentence.'
>>> sentence = first + second      #1
>>> print(sentence)
This is a sentence.
>>> print('-=' * 5)     #2
-=-=-=-=-=

Inspect the interactive Python examples showing string concatenation and repetition. section 4.3.4

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines repetition and demonstrates it interactively, without discussing implementation details or edge cases.
Examples
Demonstrates '-=' * 5 producing a repeated separator at the Python prompt.
What you can do after
Apply the * operator to repeat strings and verify the resulting output at the Python prompt.
How it connects
Connects string repetition with string concatenation and Python strings.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.