Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is string multiplication implemented in CPython?

Tags:

People also ask

How does string multiplication work in Python?

When you multiply a string by an integer, Python returns a new string. This new string is the original string, repeated X number of times (where X is the value of the integer).

How do you multiply a string list in Python?

This has probably been asked before, please read the language's docs at docs.python.org and consider using a simple "for i in range" loop where you can c += [a[i]] * b[i] in each iteration. A better duplicate target: Repeat each item in a list a number of times specified in another list.

How do you multiply a string by a number?

To (properly) multiply an string by an integer, you split the string into characters, repeat each character a number of times equal to the integer, and then stick the characters back together. If the integer is negative, we use its absolute value in the first step, and then reverse the string.

How do you multiply a string with a float in Python?

Strings cannot be multiplied by floating-point numbers. This is because multiplying strings by integer numbers creates a repetitive sequence of the string. This is not possible using a floating-point because it would result in multiplying a string by decimal values.


Python allows the multiplication of strings by integers:

>>> 'hello' * 5
'hellohellohellohellohello'

How is this implemented in CPython?

I would particularly appreciate a pointer to the source code; the Mercurial repository is a labyrinth beyond my abilities to navigate.