String formatting like Python's .format()
npm install python-format-js





Python-style string formatting for JavaScript.
This library brings Python’s powerful str.format() syntax to JavaScript, keeping the behavior as close as possible to Python.
> ⚠️ Note:
> The expected output is intentionally the same as Python’s str.format.
---
``bash`
npm install python-format-js
or
`bash`
yarn add python-format-js
`js`
// Import module + prototype
const format = require("python-format-js");
or (prototype only):
`js`
require("python-format-js");
---
`bash`
npm test
or
`bash`
yarn test
---
You can:
* Basic positional formatting
* Named placeholders (objects)
* Padding and alignment
* String truncation
* Truncation + padding combined
* Signed and formatted numbers
* Numeric bases (binary, octal, hex)
* Scientific notation
* Percent and thousand separators
📘 Python reference:
https://pyformat.info/
---
* < Left align>
* Right align^
* Center align=
* Sign-aware padding
* + Always show sign-
* Only negative sign
* Leading space for positive numbers
* , Thousand separator (comma)_
* Thousand separator (underscore)b
* Binaryo
* Octalx
* Hex (lowercase)X
* Hex (uppercase)d
* Decimaln
* Numbere
* Scientific (lowercase)E
* Scientific (uppercase)f
* Fixed-pointg
* General%
* Percentage#
* Prefix for binary, octal and hex (0b, 0o, 0x)
---
> ⚠️ Reminder: Output matches Python behavior.
---
`js`
"{} {}".format("Jhon", "Mart");
// → "Jhon Mart"
---
`js`
"My name is {0} and I am {1} years old.".format(["Jônatas", 21]);
// → "My name is Jônatas and I am 21 years old."
---
`js`
"My name is {name} and I am {age} years old.".format({
name: "Jônatas",
age: 21,
});
// → "My name is Jônatas and I am 21 years old."
---
`js`
"{}".format(2); // "2"
"{}".format(3.14); // "3.14"
"{}".format(true); // "true"
---
`js`
"{} {} {}".format(2, 3.14, true);
// → "2 3.14 true"
---
`js`
"{:<6}".format("oii"); // "oii "
"{:>6}".format("oii"); // " oii"
"{:^6}".format("oii"); // " oii "
"{:^7}".format("oii"); // " oii "
---
`js`
"{:_<7}".format("Jhon"); // "Jhon___"
"{:_>7}".format("Jhon"); // "___Jhon"
"{:_^7}".format("Jhon"); // "_Jhon__"
---
`js`
"{:^3}".format("Gustavo");
// → "Gustavo"
---
`js`
"{:.7}".format("Jonatas Martins");
// → "Jonatas"
---
`js`
"{:10}".format("test");
// → "test "
---
`js`
"{:f}; {:f}".format(3.14, -3.14);
// → "3.140000; -3.140000"
---
`js
"{:+f}; {:+f}".format(3.14, -3.14);
// → "+3.140000; -3.140000"
"{: f}; {: f}".format(3.14, -3.14);
// → " 3.140000; -3.140000"
`
---
`js`
"{:<15f}; {: f}".format(3.14, -3.14);
// → "3.140000 ; -3.140000"
---
`js`
"{:b}".format(42); // "101010"
"{:#b}".format(42); // "0b101010"
"{:>4b}".format(5); // " 101"
---
`js`
"{:o}".format(42); // "52"
"{:#o}".format(42); // "0o52"
"{:+#o}".format(4233); // "+0o10211"
"{:-#o}".format(-4233);// "-0o10211"
---
`js`
"{:x}".format(42); // "2a"
"{:#x}".format(42); // "0x2a"
"{:#X}".format(42); // "0X2A"
---
`js`
"{:e}".format(4233); // "4.233e+3"
"{:E}".format(4233); // "4.233E+3"
"{:%}".format(0.065); // "6.500000%"
---
`js`
"{:,}".format(1234567890);
// → "1,234,567,890"
---
)`js`
"{[1]}".format([1, 2, 3]);
// → "2"
---
)`js
"{:03d}".format(7);
// → "007"
"{:04}".format(8);
// → "0008"
`
---
`js`
"{:04},{:010},{:010},{:5}".format(8, 9, 6.4, "Test");
// → "0008,0000000009,00000006.4,Test "
---
) — Issue #53`js
"{:+10d}".format(-14);
// → " -14"
"{:=10d}".format(-14);
// → "- 14"
"{:=+10d}".format(14);
// → "+ 14"
"{:=+7d}".format(145678);
// → "+145678"
`
---
)`js
"This is PI: {:.4f}".format(Math.PI);
// → "This is PI: 3.1416"
"This is PI: {:.8f}".format(Math.PI);
// → "This is PI: 3.14159265"
`
---
)`js`
"{:_}".format(1234567890);
// → "1_234_567_890"
---
`js`
"{num:_^+#20,.4f}".format({ num: 12345.67890123 });
// → "____+12,345.6789____"
---
`js
const Format = require("python-format-js");
Format("{:E}", 4233);
// → "4.233000E+3"
`
) — Issue #54`js`
"My name is {name} {something}".format({
name: "Jônatas",
age: 21,
});
// ❌ Throws error
`js`
"My name is {name} {something}".format(
{ name: "Jônatas", age: 21 },
{ strict: false }
);
// → "My name is Jônatas "
---
`js``
"{}".format(); // ❌ index out of range
"{:a}".format(123); // ❌ invalid format specifier
"{2}".format("a", "b"); // ❌ index out of range
---
* Found a bug? Please report it 🙏
* Contributions are welcome!