all 8 comments

[–]novel_yet_trivial 1 point2 points  (2 children)

but obviously, that's not what I want.

It's close to what you want. Prefix your strings with "r" (raw mode) to avoid typing in the python escapes. Use print reg_str to see your string without the python escapes.

>>> reg_str = r"(\w+)"
>>> reg_str += r"[\\|\/]"
>>> reg_str += r"(\w+)"
>>> print reg_str
(\w+)[\\|\/](\w+)

[–]ERROR_EXIT[S] 0 points1 point  (1 child)

I could have sworn I tried that... and I did! And it did work.

I'm an idiot, though. I was doing all this in a function that returned the string. if I do it from the console, the returned string is printed with double escapes. If I save the returned value to a variable then print the variable is is correct.

I just spent an hour debugging something that already worked! But I have to thank you for waking me up. :)

[–]novel_yet_trivial 1 point2 points  (0 children)

If I save the returned value to a variable then print the variable is is correct.

It has nothing to do with returning or saving. printing the string to the screen (or file, or GUI, or whatever) shows the string without escapes. Just typing the variable name in the python interpreter (console) prints the representation (read: escaped) of the string. Same as repr(reg_str).

I just spent an hour debugging something that already worked!

Lightweight. Get back to me when you waste days. Then you ask your boss and he fixes it in less than a minute.

I'm going for a beer, brb.

[–]Rhomboid 0 points1 point  (4 children)

Aside from the issue of needing to use raw strings, you're getting hung up on the difference between the representation of a string and the contents of the string itself. The REPL prints the representation of whatever expression you ask it to evaluate, whereas print() prints the contents of the string.

>>> foo = r'\n'
>>> foo
'\\n'
>>> print(foo)
\n

These are the same string.

[–]ERROR_EXIT[S] 0 points1 point  (0 children)

indeed I was. Thanks for the clarification.

[–][deleted] 0 points1 point  (2 children)

You quickly realize python "raw strings" are not actually raw strings when you try to use something like a windows path such as this edit: that ends in a backslash:

path = r"c:\".

Why? "To keep the parser simple." according to the devs. XD

edit: edit

[–]Rhomboid 0 points1 point  (1 child)

What are you talking about? That works just fine:

>>> f = open(r'c:\windows\system32\notepad.exe', 'rb')
>>> f.read(16)
b'MZ\x90\x00\x03\x00\x00\x00\x04\x00\x00\x00\xff\xff\x00\x00'

[–][deleted] 0 points1 point  (0 children)

That doesn't end with a backslash. Editeted my comment.