Code:
my_string= “any string you’d like.”
print len(my_string)
print my_string.upper()
Output error:|4
Traceback (most recent call last):
File “python”, line 5, in
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 14: ordinal not in range(128)
The copy/paste was likely the issue. If you happened to catch a character that isn’t encoded as expected then you can throw errors like this (python2 defaulted to ascii). If you typed this out you would probably avoid the error as a single quote is part of ascii.
The docs on using unicode characters might help or have a web search for this issue
https://docs.python.org/2.7/howto/unicode.html
I copy pasted the solution because the simple typing it in created the same error.
The text was the same in the end.
Thanks for the try.
my try:
Write your code below, starting on line 3!
my_string= “any string you’d like.”
print len(my_string)
aaa=my_string.upper()
print aaa
result same:
Traceback (most recent call last):
File “python”, line 7, in
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 14: ordinal not in range(128)
Unless I leave off the parens after my_string.upper, then I get:
24
<built-in method upper of str object at 0x7f92ec4606f0>
If you typed it out and caught that error then your keyboard must be sending some funny characters or your browser is altering them so you may want to look into that as an issue. It will likely continue to cause problems otherwise.
This is more visible with formatted code, see- How do I format code in my posts? since the forum auto edits quotes into smart quotes too-
Can you see the difference in the following two strings, namely the '
and ‘
characters?
my_str = "any string you'd like"
my_str2 = "any string you‘d like"
The first only contains characters from the ascii standard whereas the second has a character that isn’t so the bytes can’t be read with that encoding, the link above covers why.