Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone ,
Please i want to know how does the string in print been executed
Python
print("Twinkle, twinkle, little star,\n\
\tHow I wonder what you are!\n\
\t\tUp above the world so high,\n\
\t\tLike a diamond in the sky.\n\
Twinkle, twinkle, little star,\n\
\tHow I wonder what you are!")

me i see that the backslash allows us to return to following line and he doesn't enter in the execution of the instruction .how is that work ?i need some clarifications please.

What I have tried:

searching on the net and coding
Posted
Updated 20-Sep-21 6:24am
v2

1 solution

 
Share this answer
 
Comments
Mohamed AIT HADDOU 22-Sep-21 11:34am    
Hello,i talk about the backslash only inside the print function not \n (see code) it allow to write the string in various line inside print
Richard MacCutchan 22-Sep-21 11:46am    
It's nothing to do with the print function. The backslash is used to escape certain characters inside text strings, and is handled by the compiler. And this is fairly universal in all programming languages.

See also 3. An Informal Introduction to Python — Python 3.9.7 documentation[^].
Mohamed AIT HADDOU 22-Sep-21 12:37pm    
There is not compiling in python , how the interpreter works with this instruction that is what i want to know please dear Richard ? notice that i read the documentation and i t was useful.i will be happy to have a clarification in this thing
Richard MacCutchan 22-Sep-21 12:48pm    
There is an element of compiling in Python as it interprets the source code and turns it into executable statements. And part of that includes replacing escape sequences in strings. It is easy to test:
foo = "My \name is \richard";
with open("zzz.txt", 'w') as f:
    f.write(foo)

Now open zzz.txt in Notepad or any hex editor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900