Doing variable insertion directly into the string

Nov 27, 2021 at 8:53am
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 Dec 26, 2021 at 9:35am
Nov 27, 2021 at 9:07am
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.
Nov 27, 2021 at 9:47am
string e ="FT" + to_string(ht) + "\r\n";
which, to be honest, is what I'd have done in Python.
Last edited on Nov 27, 2021 at 11:44am
Nov 29, 2021 at 6:30pm
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 Nov 29, 2021 at 6:30pm
Topic archived. No new replies allowed.