C++20 String formatting: Part-2 Width, Fill and Alignment

Gajendra Gulgulia
7 min readMar 8, 2023

In the first part of the tutorial series, I provided a CMake setup for C++ fmt library that allows to work with string formatting without installing it as a root owner. I also explained the basics of formatting library and how the placeholder {} play a central role in formatting the output and the content of the string.

In this article, I will continue on the journey of formatting string in C++20 with std::format (currently fmt::format ) . The goal of this artilce is to explain various types of format specifiers in the string formatting library

1. Format Specifiers

The cpp reference page defines the input to the placeholders in the formatting library to be of the following types:

  1. { arg-id (optional) }
  2. { arg-id (optional) : format-spec }

The first input type was already discussed in the first part of the tutorial series. The second type of input to the placeholders where the optional argument id, a colon ( : ) and a format specifier is specified.

The format specifiers are the manipulator tokens in the string formatting library that allows one to format the string in multiple ways. If you are familiar with python string formatting and string maipulation then you are in for a treat…

--

--

Gajendra Gulgulia
Gajendra Gulgulia

Written by Gajendra Gulgulia

I'm a backend software developer and modern C++ junkie. I run a modern cpp youtube channel (https://www.youtube.com/@masteringmoderncppfeatures6302/videos)

No responses yet