Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ string formatting like Python "{}".format

I am looking for a quick and neat way to print in a nice table format with cells being aligned properly.

Is there a convenient way in c++ to create strings of substrings with certain length like python format

"{:10}".format("some_string")
like image 672
chrise Avatar asked Sep 09 '25 15:09

chrise


2 Answers

In C++20 you can use std::format which brings Python-like formatting to C++:

auto s = std::format("{:10}", "some_string");

Until it is widely available you can use the open-source {fmt} formatting library, std::format is based on.

Disclaimer: I'm the author of {fmt} and C++20 std::format.

like image 140
vitaut Avatar answered Sep 12 '25 14:09

vitaut


Try this https://github.com/fmtlib/fmt

fmt::printf("Hello, %s!", "world"); // uses printf format string syntax
std::string s = fmt::format("{0}{1}{0}", "abra", "cad");
like image 42
mattn Avatar answered Sep 12 '25 14:09

mattn