In Python 2
>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)
^ This is the difference between a byte string (plain_string) and a unicode string.
>>> s = "Hello!"
>>> u = unicode(s, "utf-8")
^ Converting to unicode and specifying the encoding.
In Python 3
All strings are unicode. The unicode function does not exist anymore. See answer from @Noumenon
Top answer 1 of 13
315
In Python 2
>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)
^ This is the difference between a byte string (plain_string) and a unicode string.
>>> s = "Hello!"
>>> u = unicode(s, "utf-8")
^ Converting to unicode and specifying the encoding.
In Python 3
All strings are unicode. The unicode function does not exist anymore. See answer from @Noumenon
2 of 13
85
If the methods above don't work, you can also tell Python to ignore portions of a string that it can't convert to utf-8:
stringnamehere.decode('utf-8', 'ignore')
GeeksforGeeks
geeksforgeeks.org › python › convert-a-string-to-utf-8-in-python
Convert a String to Utf-8 in Python - GeeksforGeeks
July 23, 2025 - Both methods produce a bytes object with the UTF-8 representation of the original string. The str.encode method serves as an alternative syntax for achieving the same result ... original_string = "Hello, World!" utf8_string_encoded = original_string.encode('utf-8') utf8_string_str_encode = str.encode(original_string, 'utf-8') print("Original String:", original_string) print("UTF-8 String (Using encode method):", utf8_string_encoded) print("UTF-8 String (Using str.encode method):", utf8_string_str_encode)
Videos
DigitalOcean
digitalocean.com › community › tutorials › python-string-encode-decode
Python String Encode and Decode: Complete Guide | DigitalOcean
August 3, 2022 - Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’. Let’s look at a simple example of python string encode() decode() functions. str_original = 'Hello' bytes_encoded = str_original.encode(encoding='utf-8') print(type(bytes_encoded)) str_decoded = bytes_encoded.decode() print(type(str_decoded)) print('Encoded bytes =', bytes_encoded) print('Decoded String =', str_decoded) print('str_original equals str_decoded =', str_original == str_decoded)
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
# print string print('The string is:', string) # default encoding to utf-8 · string_utf = string.encode() # print result print('The encoded version is:', string_utf) ... The string is: pythön! The encoded version (with ignore) is: b'pythn!' The encoded version (with replace) is: b'pyth?n!' Note: Try different encoding and error parameters as well. Since Python 3.0, strings are stored as Unicode, i.e.
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.5 documentation
February 23, 2026 - The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), 'ignore' (just leave the character out of the Unicode result), or 'backslashreplace' (inserts a \xNN escape sequence). The following examples show the differences: >>> b'\x80abc'.decode("utf-8", "strict") Traceback (most recent call last): ...
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler ... The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used....
Codecademy
codecademy.com › docs › python › strings › .encode()
Python | Strings | .encode() | Codecademy
October 23, 2023 - The .encode() method takes a given string and returns an encoded version of that string. If no encoding specifications are given, UTF-8 is used by default. ... Looking for an introduction to the theory behind programming?
LabEx
labex.io › tutorials › python-how-to-use-python-utf8-encoding-451217
How to use Python UTF8 encoding | LabEx
LabEx recommends understanding UTF-8 as a fundamental skill for modern Python programming. Encoding and decoding are fundamental processes for converting text between different representations in Python. ## String to bytes encoding text = "Hello, 世界!" encoded_text = text.encode('utf-8') print(encoded_text) ## Converts string to UTF-8 bytes ## Bytes to string decoding decoded_text = encoded_text.decode('utf-8') print(decoded_text) ## Converts bytes back to string
Java2Blog
java2blog.com › home › python › python string › encode string to utf-8 in python
Encode String to UTF-8 in Python [2 ways] - Java2Blog
December 25, 2022 - The same is not the case for Python 2. In this version, bytes and string are basically the same thing. So this, function is redundant since the string is already encoded. We can observe the same in the code below. ... To encode string to UTF-8 in Python, use the codecs.encode() function.
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-python
UTF-8 in Python | Encoding Standards for Programming Languages
For instance, my_string = "你好, world!" becomes b'\xe4\xbd\xa0\xe5\xa5\xbd, world!' when encoded to UTF-8. Decoding this bytes object with utf-8 will perfectly restore the original string. A common pitfall is attempting to write a Python string directly to a file opened in binary mode ('wb') without first encoding it.
Evanjones
evanjones.ca › python-utf8.html
How to Use UTF-8 with Python (evanjones.ca)
October 1, 2005 - You can do this in one of two ways. First, you can place a UTF-8 byte-order marker at the beginning of your file, if your editor supports it. Secondly, you can place the following special comment in the first or second lines of your script: ... Any ASCII-compatible encoding is ...
Tutorialspoint
tutorialspoint.com › python › string_encode.htm
Python String encode() Method
Welcome to Tutorialspoint." str_encoded= str.encode('utf_16','strict') print("The encoded string is: ", str_encoded) On executing the above program, the following output is generated - The encoded string is: b'\xff\xfeH\x00e\x00l\x00l\x00o\x00!\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00T\x00u\ x00t\x00o\x00r\x00i\x00a\x00l\x00s\x00p\x00o\x00i\x00n\x00t\x00.\x00' The python string encode() method that takes 'euc_kr' as its encoding has a variable length encoding done on korean characters.