Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help for escape sequences
#1
Hello,

I'm new in programming and python.
So I have a question for escape sequences.

If you want to print a backslash, you have to use an escape sequence, right?
print('\\')
Output:
\
But you don't have to use an escape sequence here:
print('Tom\s car')
Output:
Tom\s car
Why?

Thank you for your help :)
Reply
#2
Because Python realizes that \s isn't a recognized escape sequence, so it leaves it as is. Using two backslashes clarifies what to do in the case of a recognized escape sequence:

>>> print('tom\s car')
tom\s car
>>> print('tom\n car')
tom
 car
>>> print('tom\\n car')
tom\n car
In the case of \s, the unneeded clarity of \\s doesn't hurt.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of escape character in re.sub and find WJSwan 1 877 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  detect equal sequences in list flash77 17 2,684 Oct-28-2022, 06:38 AM
Last Post: flash77
  Escape indentation Frankduc 11 2,982 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,141 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,743 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,271 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 1,991 Dec-23-2020, 06:47 PM
Last Post: Mark17
  needleman wunsch algorithm for two sequences of different length johnny_sav1992 0 1,677 Jul-27-2020, 05:45 PM
Last Post: johnny_sav1992
  copying parts of mutable sequences Skaperen 1 2,191 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 1,422 Nov-23-2019, 05:20 PM
Last Post: SinPy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020