-1

I'm looking for a way to declare a string that contains double quotes and single quote. In javascript I would do

let str = `"," '`

But when I try the same syntax in python, my IDE shows this error

Python version 3.7 does not support backquotes, use repr() instead

How can I use repr() to achieve this result?

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • Use escaping: `s = '\',"'` – rdas Sep 26 '19 at 21:29
  • 1
    Use the correct syntax for Python (don't make stuff up because it's seen in another language; also, the ` trick "works" in JS up until the point that interpolation-related characters are involved, then those have to be escaped). The string literals supported in Python are covered in https://docs.python.org/3/reference/lexical_analysis.html and related tutorials and resources. – user2864740 Sep 26 '19 at 21:31
  • 1
    The title is a complete red-herring, as can be quickly seen by *searching* for `repr` in the Python documentation (it just "does not apply" to the task). Then there is a more relevant question once the assumptions are updated: eg. *"How to write a string in Python that contains single and double quotes [with minimal effort]?"* – user2864740 Sep 26 '19 at 21:33
  • @user2864740 The question is not `How to write a string in Python that contains single and double quotes`!!! The question is since IDE says to use repr() hot do I use it if accordingly official documentation it takes object – Sergey Pleshakov Sep 26 '19 at 21:41
  • In python you cannot do this without escapes. `repr` automatically makes escaped string that can be used in source. But you need to provide a valid string for repr, so it is kind of chicken and egg deal. The IDE message is confusing. – ddbug Sep 26 '19 at 21:50
  • 1
    @SergeyPleshakov Please see documentation for `repr`. The IDE message would be correct ***if and only if* the code was originally correct (for some semantic meaning) to use backticks as such in *Python***. The initial assumption is wrong and the IDE message does not apply. Re: "I'm looking for a way to declare a string that contains double quotes and single quote." – user2864740 Sep 26 '19 at 21:59
  • @user2864740 your comments are red-herring. I'm not insisting to use backquotes. The question is how to use `repr()` to achieve equivalent of js \`\` – Sergey Pleshakov Sep 26 '19 at 22:02
  • 1
    @SergeyPleshakov The \`str\` syntax on JavaScript (a **string literal with interpolation**) is **not** serving the same purpose/role as `repr` (this is a function, and has nothing to do with construction of source-code string literals used to supply values) in Python, regardless of how the IDE message is being [incorrectly] interpreted. Not even close. Thanks for reading the documentation and consulting examples at this time. – user2864740 Sep 26 '19 at 22:02
  • @user2864740 yes if taking into consideration `interpolation-related characters` which I don't have in my string – Sergey Pleshakov Sep 26 '19 at 22:05
  • 1
    @SergeyPleshakov That message shows up because backticks were used in Python 2 as a shortcut to a `repr()` call to convert objects into strings. It is not exactly the same as JS's backticks. – GZ0 Sep 26 '19 at 22:07
  • @user2864740 dude, first of all, I'm new to python, no master degree in computer science either, coming here for help, because I want to get how something works. What's wrong with this? You reinterpret my question and start explaining me why \`\` is not the same as repr() which I never asked. Thanks for your time, but I get more support from others – Sergey Pleshakov Sep 26 '19 at 22:12

3 Answers3

2

The reason the error message says what it does is because backquotes have never been used in Python to do what you want. Instead, they used to be a shortcut for using the repr function, that is no longer supported.

According to documentation it take an object

Everything is an object in Python, so there is no issue there. But there is an issue in that the repr function does not do what you want.

We need to go back to the original question instead:

I'm looking for a way to declare a string that contains double quotes and single quote.

In Python, you may either escape whichever quote is the one you used for the string, for example:

"\",\" '" # using double quotes
'"," \'' # using single quotes

Or you may use so-called triple quotes:

""""," '""" # like so

But beware that this does not work if you have the same kind of quote at the end of the string:

# '''"," '''' -- does not work!
'''"," \'''' # works, but it defeats the purpose

In each case, '"," \'' is the form that Python will use to report the string back to you.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
2

The message in the IDE is referring to using backticks around a variable name or other expression. In Python 2, `someVar` was a shortcut for repr(someVar).

But this isn't really what you're trying to do. The message is simply hard-coded for any use of backticks.

You just have to escape the quotes that are the same as the string delimiter.

s = '"," \''
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I figured that out

So literally all I had to do was this

text = repr(",'") # returns this string ",'"

The part that confused me was I wasn't sure how to pass the argument to the function since according to documentation I should have passed an object, not a string or a list of string. Until I realized that a string is an object too

A few examples that helped me to understand it in details

>>> print("123")
123

>>> print(repr("123"))
'123'

>>> print(repr(",'"))
",'"
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40