Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 5
    I've tried to keep this answer focussed specifically on the "double backslash" misunderstanding, but it's also useful to know about raw string literals. Commented Jun 6, 2014 at 15:48
  • Input: 'I am 5' 6' output: ''I am 5\' 6'' I am trying so hard to find easy ways to achieve this but does not seems so easy. Any advice please? Commented Feb 15, 2023 at 22:37
  • It's not practical to answer a different question in comments, but careful reading of docs.python.org/3/tutorial/introduction.html#strings might help. Note that in order to unambiguously represent a string literal which contains both single and double quotes, Python is forced to escape at least one of those with a backslash. Note also that as explained above, the representation of a string in e.g. an interactive console is not the same as the content of that string. Commented Feb 16, 2023 at 8:58
  • Since you brought up raw string literals, it's important to be aware of one limitation of them - you can't end a raw string literal with a backslash. It's a quirk in the way they're parsed. Commented Sep 27, 2024 at 18:41