Search code examples
pythonunicoderaw-input

Python: raw_input should be converted to unicode


I get the users Input with raw_input(). This Input is stored in a variable. How can I cast this variable to be Unicode. I Need this for further executions.

 userinput = raw_input("Hello. What is your Name?")

Solution

  • Jst call the "decode" method on the result of "raw_input". Matter is, you need to know the encoding of the terminal where the input was made:

    import sys
    
    value = raw_input("bla bla bla").decode(sys.stdin.encoding or 'utf-8')
    

    But yu really should use Python 3