+ 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

9th Nov 2018, 1:04 AM
Lily Ofori-Amanfo
Lily Ofori-Amanfo - avatar
7 odpowiedzi
+ 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”
10th Nov 2018, 8:44 AM
Sokrat_Poghosyan
Sokrat_Poghosyan - avatar
+ 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
9th Nov 2018, 1:20 AM
Anya
Anya - avatar
+ 2
It'll return an empty string
9th Nov 2018, 1:16 AM
Taste
Taste - avatar
+ 2
Hey nice question If you print("a"*3) It give aaa That mean strings can be multiplied with integers Hope you get SATISFIED
9th Nov 2018, 9:46 AM
Harshit HJ
Harshit HJ - avatar
+ 1
thank you all
9th Nov 2018, 1:21 AM
Lily Ofori-Amanfo
Lily Ofori-Amanfo - avatar
+ 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
9th Nov 2018, 9:41 AM
CHERUKURI PRANEET AMUL AKASH
CHERUKURI PRANEET AMUL AKASH - avatar
+ 1
If you multiply a string with 0 python wil produce a empty string. The string wil be shown zero times
25th Sep 2020, 1:04 PM
Lucas Goossens
Lucas Goossens - avatar