0

I am translating a program from Java to Python.

In the source code of the Java program, the strings use the "\u000x" encoding for a few characters. As I understand, this is the unicode representation of the character and x is a hexadecimal code for that character.

In Java program the string is declared as:

data = "~\\GJ_F^A\u001eXJ]NK\u0018!"

How do I represent this in Python?

The above string has two encoded characters:

\u001e
\u0018

Thanks.

Neon Flash
  • 3,113
  • 12
  • 58
  • 96
  • 2
    Does this answer your question? [How to encode Python 3 string using \u escape code?](https://stackoverflow.com/questions/32280753/how-to-encode-python-3-string-using-u-escape-code) – Martheen Apr 11 '20 at 00:02

1 Answers1

1

Python also has Unicode escape codes. This is valid python:

data = "~\\GJ_F^A\u001eXJ]NK\u0018!"
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Hello. Thanks for confirming. The issue in my case was that I was using Python 2.x version which did not have support for such encoding. I used Python 3.x and it worked properly. – Neon Flash Apr 11 '20 at 08:38
  • good choice, not a good idea to do new projects in python2 - but you can use unicode escapes in unicode strings in python2 too https://docs.python.org/2/howto/unicode.html#unicode-literals-in-python-source-code – Joni Apr 11 '20 at 12:47