Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active June 28, 2020 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/62a1e32e483f3bdf41e22d05dcd86263 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/62a1e32e483f3bdf41e22d05dcd86263 to your computer and use it in GitHub Desktop.
s1= "example öf strings"
print (s1) #Output:example öf strings
#Use backslash for the character that can't be encoded
print(s1.encode(encoding="ascii",errors="backslashreplace")) #Output:b'example \\xf6f strings'
#ignores the character that can't be encoded
print(s1.encode(encoding="ascii",errors="ignore"))#Output:b'example f strings'
#replace the character that can't be encoded with the text explanining the character.
print(s1.encode(encoding="ascii",errors="namereplace"))#Output:b'example \\N{LATIN SMALL LETTER O WITH DIAERESIS}f strings'
#Replace the character that can't be encoded with the question mark
print(s1.encode(encoding="ascii",errors="replace"))#Output:b'example ?f strings'
#Replace the character that can't be encoded with xml character.
print(s1.encode(encoding="ascii",errors="xmlcharrefreplace"))#Output:b'example öf strings'
#strict-Raise Unicode Error
print(s1.encode(encoding="ascii",errors="strict"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
#errors are not mentioned.Default is strict-Raise Unicode Error.
print(s1.encode(encoding="ascii"))
#Output:UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 8: ordinal not in range(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment