In my C++ code, I am executing some commands using python as follows:
std::string word = "Something";
std::cout << word; //will execute using C++
PyRun_SimpleString("import sys"); // will execute using Python
The problem is how to pass word to Python ?
I want something like this: PyRun_SimpleString("Hello %" %word);
In Python you can do: "Hello {}".format(word) and the result "Hello Something"
I found something like this: sprintf(str, "hello %s", word);
But the problem is printf or sprintf will send it to console and will not return the value of word.
sprintfwill not send it to the console.new_word = sprintf(str, "Hello %s", word);?sprintf()returns the number of characters copied intostr. You would neednew_word = str;aftersprintf()exits. But you really shouldn't be usingsprintf()anyway. That is the C way of formatting strings. The C++ way is to usestd::string::operator+or astd::ostringstreaminstead