0

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.

Sanu
  • 123
  • 8
  • Can you please mention the exact code? I'm not able to reproduce your issue. – Shradha Oct 03 '20 at 15:29
  • ran your snippet and get - `\mu_`, exactly as expected. what excatly don't woek for you? – Yossi Levi Oct 03 '20 at 15:31
  • 1
    How are you printing it? Try printing it with print() – Brian McCutchon Oct 03 '20 at 15:33
  • Does this answer your question? [How to print a single backslash?](https://stackoverflow.com/questions/19095796/how-to-print-a-single-backslash) – Craig Oct 03 '20 at 15:33
  • 3
    `'\\mu_'` is the representation of the string `\mu_` - all seems correct here. You seem to confusing the two. – mata Oct 03 '20 at 15:34
  • @Shradha the function is exact what I defined in the above. Then I called the function with ```mu()``` in the next line. – Sanu Oct 03 '20 at 15:37
  • @Craig I don't think so. I am not facing this problem if I try to get \mu_ using print only. But when trying to use it in a function, I am getting the problem – Sanu Oct 03 '20 at 15:39
  • Now that everyone is getting this done, it feels like, there is a problem with the python version I have installed. – Sanu Oct 03 '20 at 15:41
  • It is not a problem--it is by design, as chepner explains in his answer. You are confusing the internal representation of a string, which you see inside the python interpreter, with the printed version of a string. – Craig Oct 03 '20 at 15:42
  • I think I narrowed the problem down a bit. I am getting what I want if I use print() within the function as well. The problem arises if I try to get the answer using the return command. – Sanu Oct 03 '20 at 15:47
  • @Craig I am not sure I understand exactly what you are trying to say. Honestly I am not a programmer but a student of physics. So I don't know how the code actually works as I have just began to learn python. If you could explain that would be helpful. – Sanu Oct 03 '20 at 15:49
  • It has nothing to do with `return`. `print()` the array and you'll see the backslashes show up correctly. – Brian McCutchon Oct 03 '20 at 19:58

4 Answers4

2

There is no problem here. The string you are returning contains only a single backslash; it is only the string representation of that string that uses two backslashes. Compare

>>> r'\mu_'
'\\mu_'
>>> print(r'\mu_')
\mu_
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This works fine within print. But there is a problem particularly when trying to do the same using a function. I need the function as I need to print this for varying number of times. – Sanu Oct 03 '20 at 15:43
  • What, exactly, is your perceived need for `str.__repr__` to return something else? – chepner Oct 03 '20 at 15:59
  • The interactive interpreter is not meant as a user interface for your program; you should never *care* what `str.__repr__` returns. – chepner Oct 03 '20 at 16:00
1

When printing a list in python the repr() method is called in the background. The string stored is '\', it's just represented as '\\'.

If you print out every element individually, it will show you the literal value as opposed to the represented value:

print(ten(4, 1)[1])

Output:

u^\mu_2
Shradha
  • 2,232
  • 1
  • 14
  • 26
  • 1
    okay. I understand the concept. Now I have to find a way to use this. Thanks a lot. – Sanu Oct 03 '20 at 18:37
1

You can also try to pass its charater code to chr

print(chr(92))
AMC
  • 2,642
  • 7
  • 13
  • 35
Mandar_Kat
  • 11
  • 1
0

Use:

a = r"\"

instead of:

a = "\\"
Moldytzu
  • 25
  • 9
  • I am getting an error like : ``` File "", line 2 a = r"\" ^ SyntaxError: EOL while scanning string literal``` – Sanu Oct 03 '20 at 15:33
  • A raw string cannot end with a backslash, because backslashes still escape the kind of quote used to define the literal. – chepner Oct 03 '20 at 15:35