0

I want to use single quote in a variable name and keyword. I know how to do this for a variable value using single and double quotes("5'_Exon_Annotation") or using black slash to escape the single quote. But not sure how to do this with variable name/ key word. Below are examples of how I want to assign values to column names and a variable that needs single quote.

    5'co-ordinate = "something" 

    gene_df = gene_df.assign(gene_position = gene_pos,
                             5'_co-ordinate =  bkpt1_list[1],
                             3'_co-ordinate = bkpt2_list[1])

First of all is it possible in Python? I couldn't find a post related to this.

5
  • 7
    This is not possible. One of the rules for Python variables: A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). Commented May 18, 2020 at 21:05
  • 1
    An idea is to spell out prime or _prime_ instead of the single quote.
    – Arne
    Commented May 18, 2020 at 21:08
  • That is not possible as Scott said. though maybe you can use a dictionary's key value pair.
    – shivankgtm
    Commented May 18, 2020 at 21:08
  • @ScottBoston It actually is kind of possible using globals() or locals(), see my answer.
    – mapf
    Commented May 18, 2020 at 21:23
  • 1
    Python object names are not limited to alphanumeric. Since Python 3 you can use Unicode characters like déjà_vu = 42;print(déjà_vu) or あいうえお='Japanese vowels';print(あいうえお). This makes it easy for non-English beginners to name objects, but it would block people to participate in a project if the character is not available on their keyboard ! For this question you would tempted to use characters that look like quotes like U+2018 LEFT SINGLE QUOTATION MARK, but they are forbidden by Python syntax, to prevent security problems. See PEP-3131 for more python.org/dev/peps/pep-3131 Commented May 19, 2020 at 17:20

2 Answers 2

2

While I wouldn't recommend it at all, it is possible to use all kinds of characters for attributes of classes using setattr(), like so:

class Test:
    def __init__(self):
        setattr(self, 'this variable has ""', 1)


test = Test()
print(getattr(test, 'this variable has ""'))

# test.this variable has "" does NOT work

But I really wouldn't recommend it. The intestesting thing about this is, that setting an attribute this way makes it kind of hidden, because you can only access it using getattr().

If you use globals() or locals() you can also set variables this way:

locals()['this variable has ""'] = 1  # or globals()
print(locals()['this variable has ""'])

# this variable has "" = 2 does NOT work

That makes it really cumbersome to access the variable though, and again, I really wouldn't recommend it. It's a neat trick (debateable I guess) if you really want to hide something though.

1

Here are the available characters for Python identifiers. You might find something which is close to an apostrophe but not the common one.

Btw. I'd not suggest to deviate from the ASCII table. If the language naturally support it and the editors offer helpers to write those, it's fine (like Julia) but for Python it can be quite confusing for others.

https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Here is an example of a valid Python code using some kind of a quote in the function name (copy-paste it, but the single quote is for example not displayed in Safari on macOS, however it's there as you can see in the screenshot of my terminal):

In [1]: def f֜(x): return 2*x

In [2]: f֜(3)
Out[2]: 6

Unicode char in function name

2
  • 1
    huh? Python works with any utf-8 source code just fine - it's the default. And really, alternate encodings can be used if declared at the top of the source code file. Commented May 18, 2020 at 21:09
  • Yes, but the single quote is reserved and not valid in identifier names. I just added an example.
    – tamasgal
    Commented May 18, 2020 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.