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

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");

In C++20 you'll be able to use std::format which brings Python-like formatting to C++:

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

Until then 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.


You have many options here. For instance using streams.

source.cpp

  std::ostringstream stream;
  stream << "substring";
  std::string new_string = stream.str();