[Tutor] Uses for backslashes

ThreeBlindQuarks threesomequarks at proton.me
Sun Jun 25 09:11:02 EDT 2023


Welcome Jack,

I see Alan has answered both of your questions. I note both questions you ask suggest you should look into some of the many ways Python represents strings. There are quite a few flavors that can be chosen based on convenience and taste.

Since you seem to be taking a course of sorts, it probably is explaining quite a bit and the examples often are there to illustrate. But note that many coders would rarely use a backslash to mask a quote character as there are other ways. 

If you know you will want actual double quotes in a string then you can use single quotes to delineate the string and any double quotes inside it are now unambiguous. Or you can use triples of either single or double quotes before/after and anything inside that is not the same triple is left alone.

There are other ways. And note the backslash is used for MANY things and some code with strings that may be repeatedly evaluated may have \\ and even \\\\ in it.

Each language has conventions and you just have to get used to the rules.

- Q





Sent with Proton Mail secure email.

------- Original Message -------
On Sunday, June 25th, 2023 at 3:05 AM, Jack Simpson <simpsonrmz41 at gmail.com> wrote:


> Hi,
> 
> I'm going through some quizzes and tutorials in a coding course I am
> completing and these backslashes are being used in some lines of code. I
> have researched and have found that it changes the meaning of the
> following character i.e. \n is a line feed, \t is a new tab but I can"t
> find why it is used in a case like this. Thanks for your help.
> 
> var1 = "my computer" >= "my chair"
> 
> var2 = "Spring" <= "Winter"
> var3 = "pineapple" >= "pineapple"
> 
> 
> print("Is \"my computer\" greater than or equal to \"my chair\"? Result: "
> , var1)
> print("Is \"Spring\" less than or equal to \"Winter\"? Result: ", var2)
> print("Is \"pineapple\" less than or equal to \"pineapple\"? Result: "
> , var3)
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list