+ 3
Multiplying strings by integers in Python
What really happens when you multiply a string by 0? I don’t think I really understood the outcome from my lesson. Thanks
7 Antworten
+ 2
Have you heard about operator overloading ? or generic functions ?
let’s say we are writing:
s = “aaa” * 2
compiler looks for a multiply function, which has parameters string and number, then it sends our arguments to it like mult(“aaa”, 2), after that there is a loop which will merge our string to himself like
tmp = s
tmp = tmp + tmp, as many times as second argument is, then returns the new string (tmp), that’s it. In case of 0, it returns empty string, you can check it by plusing another string in the end like this:
s = “aaa” * 0 + “bb”
you will get “bb”
+ 4
By zero the string will not be outputted. if you multiply by 100 the string will be outputted 100 times.
Check here:
https://code.sololearn.com/ck2xSqXNkXak/?ref=app
+ 2
It'll return an empty string
+ 2
Hey nice question
If you print("a"*3)
It give aaa
That mean strings can be multiplied with integers
Hope you get SATISFIED
+ 1
thank you all
+ 1
It gives the output of the string how number of times u mention.
For eg:str*n
Here the string will be printed n times
In u r case it should 0
+ 1
If you multiply a string with 0 python wil produce a empty string. The string wil be shown zero times