NodeJS String format like Python?

2019-06-16 04:16发布

问题:

In python, I can do the following:

name = "bob"

print("Hey, %s!" % name)

Is there anything similar to that (or Python's .format()) in JavaScript/NodeJS?

回答1:

sprintf should do what you are asking for I think.



回答2:

You can use util.format, it's printf like function.



回答3:

Another option is template strings

For example:

const name = "bob";
console.log(`Hey, ${name}!`);