3

In Python I can automatically create an unicode object by prepending an u (as in u"test").

Is it possible to build something like that myself?

Dakkaron
  • 5,930
  • 2
  • 36
  • 51
  • That is not possible, the special treatment of string prefixes is hardcoded in the language implementation. – user4815162342 Dec 15 '15 at 15:32
  • `r'...'` is a regular string, just without backslash interpolation, etc. It is commonly used for strings which contain regular expressions, among other things, but it's still just a string. – tripleee Dec 15 '15 at 15:34
  • @triplee: thanks for pointing that out! – Dakkaron Dec 16 '15 at 14:07

2 Answers2

1

All things are possible - but in this case, only by modifying the source code of the Python interpreter and recompiling.

A related question with the same answer: Can you add new statements to Python's syntax?

Community
  • 1
  • 1
jez
  • 14,867
  • 5
  • 37
  • 64
  • I see, thanks for that answer! Kinda disappointing since python usually allows me to change pretty much everything else. – Dakkaron Dec 16 '15 at 14:06
0

Yes you can.

All you need to do is type the following:

ur"\u<hex>"

For example, if you were to type

print ur"\u0186"

it would output the following character (given you are using a certain font)

҉

To print this, you could just simply type

print "҉"

but for this to be allowed, you must put the following line of code as the FIRST line of code

# -*- coding: utf-8 -*-

Yes, I know it has the # symbol, that is supposed to be there. Hope this helps! Have fun with unicoding! :)

  • 1
    Useful info for working with unicode in Python, but I don't think that's what the OP is asking. I think the question is, "can I make my own novel type of string literal, let's say `m'...'`, with custom-defined rules and behaviour?" – jez Dec 15 '15 at 16:06
  • It is useful info indeed, but as jez pointed out, it's not what I was asking for. jez had the right interpretation of my post. Thanks for the answer anyways though! – Dakkaron Dec 16 '15 at 14:05