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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With