A C++17 single-header library that provides a print() function similar to Python's, with support for multiple arguments, custom separators, and automatic formatting for containers.
- Single Header: Just include
pyprint.hand you're ready to go. - Variadic Arguments: Pass any number of arguments to
print(), just like in Python. - Custom Parameters: Supports
sep(separator),end(end of line), andout(output stream) parameters, mimicking Python'sprintfunction. - Automatic Formatting:
- Prints standard types (e.g.,
int,double,const char*,std::string). - Prints iterable containers or container adaptors (e.g.,
std::vector,std::list,std::array,std::queue) in a[item1,item2,...]format (comma-separated, no spaces). - Prints
std::pairandstd::tuplein a(item1,item2,...)format (comma-separated, no spaces). - Prints
std::bitsetin its binary format.
- Prints standard types (e.g.,
- Custom Print Formatting:
- Without causing a function redefinition, overload
operator<<(std::ostream&, Type)for any type to implement a custom print format (this can override existing formats).
- Without causing a function redefinition, overload
- C++ 17: Requires a C++17 compatible compiler (uses
if constexpr,std::void_t, etc.).
- Download
pyprint.h. - Include it in your project:
#include "pyprint.h" - Call the function using
pyprint::print(...).
To use custom parameters (sep, end, out), pass a pyprint::params struct as the last argument.
#include "pyprint.h"
#include <vector>
#include <string>
#include <map>
#include <sstream>
int main() {
using pyprint::print;
using pyprint::params;
// 1. Basic usage with multiple arguments
print("Hello", "world", 2025);
// Output: Hello world 2025
// 2. Using a custom separator (sep)
print(1, 2, 3, 4, params{.sep = ", "});
// Output: 1, 2, 3, 4
// 3. Using a custom end string (end)
print("No new line here.", params{.end = " "});
print("See?");
// Output: No new line here. See?
// 4. Printing a vector
std::vector<int> v = {10, 20, 30};
print("My vector:", v);
// Output: My vector: [10,20,30]
// 5. Printing a pair
std::pair<std::string, int> p = {"Test", 123};
print("My pair:", p);
// Output: My pair: (Test,123)
// 6. Printing a tuple
auto t = std::make_tuple(1, 'a', 3.14, "tuple!");
print("My tuple:", t);
// Output: My tuple: (1,a,3.14,tuple!)
// 7. Printing a map (which is an iterable of pairs)
std::map<std::string, int> m = {{"one", 1}, {"two", 2}};
print("My map:", m);
// Output: My map: [(one,1),(two,2)]
// 8. Redirecting output to a stringstream (out)
std::stringstream ss;
print("This goes to a string", params{.out = ss});
print("This goes to cout");
// Output (to cout): This goes to cout
// (ss now contains "This goes to a string\n")
// 9. Combining parameters
print("Vector", v, "and Tuple", t, params{.sep = " | ", .end = "\n---\n"});
// Output: Vector | [10,20,30] | and Tuple | (1,a,3.14,tuple!)
// ---
return 0;
}