[Tutor] Uses for backslashes

dn PythonList at DancesWithMice.info
Sun Jun 25 19:44:30 EDT 2023


On 25/06/2023 19.05, Jack Simpson 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)


"I can"t find why it is used in a case like this."

What happened when the code was executed?
What was the appearance of the output?

It seems that you have been using the Python-REPL. These questions are 
exactly its purpose - to experiment, to prototype, to check/test one's 
understanding or recollection...

Such is the opposite of the ?good, old, days when "turn-around" took so 
long that experimenting was very expensive. So, we researched the theory 
and asked questions first ...


"Escape Sequences in Python" 
(https://www.freecodecamp.org/news/escape-sequences-python/) this 
article is a gentle introduction to escaping characters.

NB further to @Alan (maybe I didn't notice) and perhaps because we 
generally eschew the use of Regular Expressions (RegEx[p]), the other 
most common use for back-slashes (that we see) is from the one or two 
colleagues who seem to like using them for line-continuation purposes. 
(I think it ugly, but then they don't like having to open (and close) 
greater numbers of parentheses - so, opinions vary...)

There's a bit of a 'cheat sheet' in "Escape Characters" 
(https://python-reference.readthedocs.io/en/latest/docs/str/escapes.html), 
which may help to highlight the (likely) few escape codes you'll ever 
(want to) use.

Which brings me to extend the observation about 'ugly' (above) - I find 
the use of back-slash escaped-characters within code-blocks not only 
ugly, but one of those things that cause these aged-eyes to struggle. In 
short, they fall into a category called "magic numbers" (or, in this 
case, "magic strings"). Received-wisdom is to 'lift' constants (the 
Python term is "literals") from inside the code to 'the top' - in the 
same way as PEP-008 recommends we do the same for import-statements. The 
reason is that when such values are required to change (and the client 
will inevitably come to 'change the rules [supposedly cast in stone]' 
one day!) it is quite a job to search through an entire code-base - and 
to be sure that you have found every last place where such might be used.

Accordingly, the solution illustrated in the web.ref:
- at the top of the code/in a separate config file (if used), define:

NEWLINE = "\n"  # or chr( 10 )*

- then within the code, use the literal-identifier:

print( F"{name}{NEWLINE}{rank}{NEWLINE}{serial_number}" )
or
print( name, rank, serial_number, sep=NEWLINE )

(not sure if your course has covered F-strings - or print()'s 
sep-argument for that matter)


* TBH when I first started in computing (after my pet-dinosaur grew too 
big to play-with) most of us memorised the 'blocks' of ASCII-code (and 
IBM mainframe EBCDIC), so I still think of NEWLINE (or rather, 
LINE-FEED) as integer-10. Also, if writing it, rather than the 
full-word, using the abbreviations mentioned in that document (eg "LF"). 
So, the encouragement to use the full-word is a little "do as I say, not 
as I do". Hah!

BTW the only ones I can remember using recently have been LF and TAB. On 
MS-Windows CR (or CR + LF) may be applicable. Can't recall using the 
others (in the ASCII context).

Where 'things have changed' is when clients want to use various Unicode 
characters. Two recent examples involved 'decorating' data in degrees 
(curiously, one client recording temperature and the other talking 
lat-long locations).

DEGREES = "\u00B0"  # ie °-symbol

Again, (mea culpa!) I tend to 'cheat' (and prefer the more 
illustrative/easier to comprehend approach) by copying the character 
from somewhere else (my Linux OpSys offers a "Character Map" application 
which enables searching for characters or scanning for what is required):

DEGREES = "℃"


Enough?

More on this, eg 
https://www.fileformat.info/info/unicode/char/2103/index.htm

If you're a glutton-for-punishment, the docs which were generated when 
Python moved from an ASCII to a Unicode 'encoding' were distilled into 
https://docs.python.org/3/howto/unicode.html#the-string-type

Very complicated for a beginner, but if you'd like to dive 'into the 
weeds' of Python's definitions: 
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

-- 
Regards,
=dn


More information about the Tutor mailing list