# Py{} adds a none in the code

I have the following MWE using pythontex:

%& -shell-escape
\documentclass{article}
\usepackage{pythontex}
\newcommand{\printPython}[1]{
\py{print(r'Hello, #1')}%
\newline
}
\begin{document}

Say hello Python:

%\begin{python}%
%print r"Hello \LaTeX!"
%\end{python}%
\printPython{4}
\begin{pycode}

print(r"\centering")
print(r"\textit{A message from Python!}")
\end{pycode}

\end{document}


When compiling, the result is

Say hello Python:
Hello, 4 None
A message from Python!


Where does the None come from, and how do I remove it?

• I solved it by replacing \py with \pyc, but nevertheless I am interested in the reason here. – arc_lupus Mar 12 '16 at 11:15

\py inserts a string representation of whatever it is given. In this case, it was given the print() function, which returns None. So you get what was printed, plus the return value. Basically, \py is a shortcut so that using print() isn't necessary. You could just use \py{r'Hello, #1'}%.
\pyc is the inline equivalent of the pycode environment. It just executes code. So if you want it to insert anything in the document, you need to use print().