#Python Code
characters = "Hello World!"
print(characters * 4) #it Will Print Hello World! 4 times
// Javascript Code
characters = "Hello World!"
console.log(characters * 4)
Asked
Active
Viewed 215 times
-1

David
- 208,112
- 36
- 198
- 279
-
I don't know the reasoning why Python *would* allow this, but I'm not familiar with much of the philosophy of Python. In JavaScript this "doesn't work" simply because performing math on text makes no sense. – David Sep 30 '21 at 11:29
-
@David please, let's not start discuss JavaScript and "makes sense" :) Anyway, you can't argue that `string * 4` does not work in JS because "performing math on text makes no sense" while JS allows `string + 4` (which Python does **not** allow, BTW). It just that `*` is not **defined** in JS for string and integer operands, not that "it makes no sense" – DeepSpace Sep 30 '21 at 11:39
2 Answers
0
In javascript you can use the inbuilt property 'repeat' of a string
characters = "Hello World!";
console.log(characters.repeat(4));

Benoit Drogou
- 969
- 1
- 5
- 15
0
You can use repeat()
.
const chorus = 'Hello World! ';
console.log(`${chorus.repeat(4)}`);
Edit: Use the other person's reply instead of mine, it's way simpler

Dương Duy Nhật Minh
- 101
- 1
- 10