String Formatting with %, Part 2


#1



when run this code i get next wrong
CODE:
nombre = raw_input("¿Cuál es tu nombre?")
mision = raw_input("¿Cuál es tu misión?")
color = raw_input("¿Cuál es tu color favorito?")

print "Ah, asi que tu nombre es %s, tu mision es %s, \
y tu color favorito es %s." % (nombre, mision, color)

WRONG:

Traceback (most recent call last):
  File "python", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)


#2

This seems like a problem with how python deals with less common characters such as ¿. I think the easiest way to fix this issue is to add a u before any strings which contain non-English characters.

nombre = raw_input(u"¿Cuál es tu nombre?")
mision = raw_input(u"¿Cuál es tu misión?")
color = raw_input(u"¿Cuál es tu color favorito?")

print "Ah, asi que tu nombre es %s, tu mision es %s, \
y tu color favorito es %s." % (nombre, mision, color)

Basically, adding the u allows you to use non-English characters in your strings.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.