I am trying to print something with a function in python using Jupyter notebook. As an example, let's say I want a function that prints '\x' where 'x' is the argument of the function.
def mu():
a="\\"
b="mu_"
return (a+b)
This should give me '\mu_' as the output. But it is giving me '\\mu_' as output. I had tried with single '\' first, but there was an error. I found an answer for that problem here in the stack overflow, which allows to print a single '\' by using the double backslash i.e.'\\' But that was for print() command only, and not inside a function. This doesn't work inside a function. Is there a way to make the above function work as I want and get a single '\' in the output as '\mu_'?
EDIT: This happens with any odd number of backslashes. But when I use even number of backslashes, I get back the same even number of backslashes. For odd number the error is:
File "<ipython-input-25-e1de4609aed4>", line 2
a='\\\'
^
SyntaxError: EOL while scanning string literal
For the above error, I defined a = '\\\'
inside the same function as above.
I hope I could express the issue clearly, if not please let me know in the comments.
P.S.- Please note that this question is not identical to some other questions here. The problem is particularly with the 'return' command used inside a function. I just realized that. If I use print(), it works as I want (which has been asked before in this platform.) but I want to know if it is possible to make it work using function.
Thus the narrowed down version of the problem has to do with the return command.
EDIT: It is essential for me to solve the problem using a function as I need to append this '\mu_' in an array. printing it is not enough. See the following code:
def ten(n,r):
tiu = []
for i in range(1,n+1):
index = str(i)
u = 'u^' + '\mu_' + index
tiu.append(u)
return (tiu)
In the next cell I type:
ten(4,1)
which gives the output:
['u^\\mu_1', 'u^\\mu_2', 'u^\\mu_3', 'u^\\mu_4']
Now if I use 2 '\' in the function, nothing changes in the output. But if I use 3 '\', I get 4 '\' in the outputs.