Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The use of escape char \
#1
Hi

I just was playing with strings in interpreter and some stranger result came when insert / escape char between two comma "" a screenshot (attached)
[Image: JxpmxHz]
Python version
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03)
Reply
#2
\a is a special character

If you want \a in the string you have to escape the escape character too
>>> '\\a'
'\\a'
>>> r'\a'
'\\a'
>>> '\a'
'\x07'
The result of the double backslash is actually what you want as it is escaping the escape character. It shows the final output. It does this in the python interpreter, but not when you print it.
>>> print('\\a')
\a
>>> print('\a')

>>> print(r'\a')
\a
Recommended Tutorials:
Reply
#3
Thanks this solved it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Escape indentation Frankduc 11 2,977 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,140 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,740 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,268 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 1,990 Dec-23-2020, 06:47 PM
Last Post: Mark17
  How to replace on char with another in a string? korenron 3 2,293 Dec-03-2020, 07:37 AM
Last Post: korenron
  How to remove char from string?? ridgerunnersjw 2 2,481 Sep-30-2020, 03:49 PM
Last Post: ridgerunnersjw
  help for escape sequences NewPi 1 1,997 Dec-11-2019, 11:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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