6

I'm attempting to join a tuple of two strings using the .join method as follows.

>>> my_tuple = ("parent", "child")
>>> "\\".join(my_tuple)

I would expect this to return parent\child, however, it returns parent\\child.

Why is this? Escaping the backslash with another backslash works fine if I attempt to simply print it.

>>> print "parent\\child"
>>> parent\child

Observed in Python 2.7.3 on Windows 7.

Evidex
  • 63
  • 1
  • 5
  • [str vs. repr](http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python) -- the first is using the `repr` of the string, which always includes escapes for unambiguity. – nmclean Apr 03 '14 at 11:45

1 Answers1

4

You got that right, it's only printing a double backslash because you are not printing it:

>>> '\\'.join(my_tuple)
'parent\\child'
>>> print '\\'.join(my_tuple)
parent\child

it's the same difference than __str__ and __repr__:

>>> '\\'.__repr__()
"'\\\\'"
>>> '\\'.__str__()
'\\'
fredtantini
  • 15,966
  • 8
  • 49
  • 55