Doing variable insertion directly into the string

Hell everybody, am disturbed on how to get the C ++ equivalent code of the below python code:

1
2
3
4
5

ht = 10

e = 'FT{}\r\n'.format (ht)


Any available tip will not go unappreciated.
https://www.theengineeringprojects.com/2019/11/introduction-to-polymorphism-in-c.html
Last edited on
C++20: #include <format>

1
2
3
4
int ht = 10 ;

// https://en.cppreference.com/w/cpp/utility/format/format
std::string e = std::format( "FT{}\r\n", ht ) ; 


The format specification is based on that in Python.
string e ="FT" + to_string(ht) + "\r\n";
which, to be honest, is what I'd have done in Python.
Last edited on
Just as a Python FYI, f-strings are more elegant, and easier to read, than using the format() function:

1
2
3
ht = 10

e = f'FT{ht}\r\n'
Last edited on
Topic archived. No new replies allowed.