-2

I have to code a program that translates a string into a UNICODE equivalent. Please tell me how to fix:

inp = input ("\nPLEASE ENTER A CHARACTER OR STRING:  ")
        display = [c for c in inp]
        uni = [unicode(c) for c in inp]
        print ("\nTHE CHARACTER(S)", display, "ARE REPRESENTED IN UNICODE AS", uni, ".")
Ethan KAO
  • 1
  • 1
  • 2

1 Answers1

0

You can convert every character of string

public class StringUnicode {

    public static void main(String[] args) {

      String foreignText = "برنامه نویسی";

      String unicodevalue="";
      for (int i = 0; i < foreignText.length(); i++) {

          unicodevalue= unicodevalue+"\\u" + Integer.toHexString(foreignText.charAt(i) | 0x10000).substring(1);

    }

        System.out.println( unicodevalue );
       // System.out.println(response);
    }
}

output:

\u0628\u0631\u0646\u0627\u0645\u0647\u0020\u0646\u0648\u06cc\u0633\u06cc

in Python

Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.

if isinstance(unicode_or_str, str):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True

You may want to read the Python 3 porting HOWTO for more such details. There is also Lennart Regebro's Porting to Python 3: An in-depth guide, free online.

Last but not least, you could just try to use the 2to3 tool to see how that translates the code for you.

NameError: global name 'unicode' is not defined - in Python 3