2

Why dont the escape sequences work in tuples when printed

 x = ("a\n", "b\n", "c\n") 
 y = ("a\n" "b\n" "c\n")
 print (x)
 print(y)

Why does it print(x) return ('a\n', 'b\n', 'c\n') and print (y)

a 
b
c
James
  • 23
  • 4
  • 4
    It's printing the data as a tuple. Try `print(''.join(x))` to print as text. – Mike67 Sep 28 '20 at 01:37
  • 1
    Each object type has its own way to convert to a string if asked. A tuple returns a `repr`esentation which can (in simple cases) be used as it is as Python code to recreate the tuple. For this the tuple also asks its items to show their Python representation (even strings) instead of the output for printing. See functions `str` and `repr` for the difference. – Michael Butscher Sep 28 '20 at 01:51
  • @MichaelButscher so tuple uses repr to represent strigns and string use str function to represent string? – James Sep 28 '20 at 02:28
  • @James Right. More exactly `print` asks each object for its `str` representation which is same as `repr` for tuples. The Python designer probably thought that it wouldn't make sense to define a real `str` function for tuples and many other datatypes because everyone would expect another human readable representation for them. – Michael Butscher Sep 28 '20 at 03:13
  • @James no, `str` objects, all objects, use `__repr__` or `__str__` depending on what you are doing. `tuple` objects, and other built-in containers like `list`, `dict` etc use the `__repr__` of the objects they contain to construct both of their own `__repr__` or `__str__` outputs. – juanpa.arrivillaga Sep 28 '20 at 03:27

4 Answers4

1

The escape sequences do work. x is being printed as a tuple because it is a tuple. If you want to join its elements, use str.join(), or have print() join it for you.

>>> x = ('a\n', 'b\n', 'c\n')
>>> x
('a\n', 'b\n', 'c\n')
>>> ''.join(x)
'a\nb\nc\n'
>>> print(''.join(x))
a
b
c

>>> print(*x, sep='')
a
b
c

>>>

Meanwhile, y is a string due to string literal concatenation

>>> y = ("a\n" "b\n" "c\n")
>>> y
'a\nb\nc\n'
>>> print(y)
a
b
c

>>> 
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • like im trying to understand why tuple does that – James Sep 28 '20 at 01:48
  • @James the answer to "why" is "because that is what the implemetora of the `tuple` object decided to do" you can create your own type of containers and choose to do things differently if you want – juanpa.arrivillaga Sep 28 '20 at 03:28
0

Try this

print("a{nl}"
      "b{nl}"
      "c".format(nl="\n"))
Zeta
  • 46
  • 7
  • 2
    im not trying to do that, im asking why tuples behavior is like that when using escape – James Sep 28 '20 at 01:46
0

Because ("a\n", "b\n", "c\n") is a tuple, but ("a\n" "b\n" "c\n") is a string:

("a\n" "b\n" "c\n") is the same as "a\n" "b\n" "c\n" what is in turn the same as "a\nb\nc\n".

(The "Hello, world." string literal is possible to write as "Hel" "lo, wor" "ld.", no + between parts.)


Now, the print() function prints a non-string object (as e.g. your tuple) converting it first to a string applying its .__str()__ method (which produces the same result as the standard function str()).

The result of str(("a\n", "b\n", "c\n")) is the string "('a\\n', 'b\\n', 'c\\n')"no newline characters in it, as you may see, it consists of these 21 characters:
              ( ' a \ n ' , ' b \ n ' , ' c \ n ' )


By contrast to this string representation of your tuple, your string ("a\n" "b\n" "c\n") alias "a\nb\nc\n" consist of 6 characters, and 3 of them are newline characters:

              a \n b \n c \n

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • MarinD, so tuple uses repr to represent a str object? and string use str()? – James Sep 28 '20 at 02:41
  • No, both use `str()`. The difference is that in the string representation of tuple there are no newline (`\n`) characters — there are instead **two individual** `\ ` `n` characters. – MarianD Sep 28 '20 at 02:46
  • Try this (with your `x` tuple) to see it clearly: `for i in (str(x)): print(i)` – MarianD Sep 28 '20 at 02:56
  • thank you i dont understand why does the \\ come from? – James Sep 28 '20 at 03:27
  • ``\\`` is the escape sequence for **one** backslash. It says something as "I'm the true backslash, and not the start of an escape sequence." So ˙`\\n` means **2 symbols** — one backslash and the letter `n`, while `\n` means **1 symbol** — the newline character. – MarianD Sep 28 '20 at 03:47
  • There is the difference, which character really **is** in the string, and **how** it is displayed for us, people (or **how** we write it in Python). With regular characters (e.g. letters) there is no problem, but how to display / write invisible symbols (as tabulator)? By escape sequences. So in the string is really stored **one** character (e.g. the tabulator), but we write it in the form of **two** characters (`\t`, i.e. the backslash and the letter `t`). So in the string `"\tx\n"` I see 3 characters - the tabulator, the letter `x` and the newline character. – MarianD Sep 28 '20 at 04:02
-1

By Default \n takes a new line no matter what you are storing, the interpreter understands this thing that you want to store some values in the next line. So, that's the way it printed like that.

Anshul Singh Suryan
  • 878
  • 1
  • 11
  • 15
  • There are two types of String formats first one is str.format() and another one is f or F. It is nothing but String formating so why it is behaving like \n as new line. And you are using f formatting in this set. – Anshul Singh Suryan Sep 28 '20 at 01:52