i would like to create a string in the following exact format :
"\"\"\"\nThis is a beautiful world.\n"
But the code :
test ="\"\"\"\nthis is a beautiful world.\n"
test
gives the output : '"""\nthis is a beautiful world.'
please help in getting an exact text. My string test should look exactly like the string it has been initialized to. but after initialization, it actually gives the output as mentioned. i want to concatenate the test string to another string
CodePudding user response:
the symbol "\" is called an escape character in most programming languages. this is used to add symbols to a string that might not be easy to add. eg - to add a double quote into a string, we add the \" to the string. eg -
a = "he said, \"hello\" to me"
this would give the output as -
he said "hello" to me
here, the "\" acts as a symbol for the code which allows it to recognize characters which might raise errors other-wise.
to include a backslash in your code, add an extra backslash to it. eg -
a = "\\"
here, the value of a is \.
if you still haven't been able to understand it, try - this tutorial
for your code,try this -
test = "\\\"\\\"\\\"\\nThis is a beautiful world.\\n"
and if you also want the double quotes at the ends,
test = "\"\\\"\\\"\\\"\\nThis is a beautiful world.\\n\""
CodePudding user response:
The first thing to note is, that just typing in the variable name when running python
interactively returns the canonical string representation of the object and not (necessarily) the plain value of the object.
For strings this means (among other things) that quotes are added around the output (in your example the outermost single quotes) and any newlines are replaced with "\n".
This means that, although your output does show "\n" the actual string contains a newline character in its place.
The check what a string looks like, you should use the print()
function to, well, print it.
>>> test = "\"\"\"\nthis is a beautiful world."
>>> test
'"""\nthis is a beautiful world.'
>>> print(test)
"""
this is a beautiful world.
>>>
Also, when running the code from a file, lines just containing variable names will not result in any output.
To answer the question
There are a few ways to handle that. Assuming that the desired output is
"""\nThis is a beautiful world.\n
i.e. the outermost double quotes are not supposed to be part of the string, that is
While using double quotes (
"…"
) to denote strings: escape any\
or"
by prepending it with\
:>>> test ="\\\"\\\"\\\"\\nthis is a beautiful world.\\n" >>> print(test) \"\"\"\nthis is a beautiful world.\n
Within regular strings
\
is used to designate control character. For example:\n
is interpreted as newline,\b
would be a backspace. If you need to have a\
in a string, you need actually write two\\
.If you are usually using
"…"
for string notation, this allows for a more consistent coding style but it is (especially in this case) quite ugly and might be hard to understand at a glance.As your string contains a lot of
"
characters, just use single quotes ('…'
) to designate the string. This removes the need to escape"
:>>> test = '\\"\\"\\"\\nthis is a beautiful world.\\n' >>> print(test) \"\"\"\nthis is a beautiful world.\n
This is less consistent (if
"…"
is usually used for strings, but allows the code to be quite a bit closer to the desired output.Use raw strings (
r'…'
orr"…"
) to disable the interpretation of control characters and allow the use of"
within the string:>>> test = r'\"\"\"\nthis is a beautiful world.\n' >>> print(test) \"\"\"\nthis is a beautiful world.\n"
or even
>>> test = r"\"\"\"\nthis is a beautiful world.\n" >>> print(test) \"\"\"\nthis is a beautiful world.\n"
This allows the code to be identical to the desired output, but it has some limitations when it comes to freely mixing
"
and'
within a single string as it is not possible to escape the quotation marks within the string without also adding\
to the string output. This can be seen in the second example, where we use\"
to escape the double quote withinr"…"
in the code but where the\
s are still present in the output. While this works well in this specific case, I would recommend against using\'
withinr'…'
or\"
withinr"…"
to avoid confusion.