user775171
user775171

Reputation:

NodeJS String format like Python?

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?

Upvotes: 12

Views: 17374

Answers (3)

nickool
nickool

Reputation: 844

Another option is template strings

For example:

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

Upvotes: 7

Alex Ivasyuv
Alex Ivasyuv

Reputation: 8844

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

Upvotes: 15

Jakob Bowyer
Jakob Bowyer

Reputation: 34718

sprintf should do what you are asking for I think.

Upvotes: 2

Related Questions