Experimenting with unicode

iacobus 0 Tallied Votes 155 Views Share

Here are five simple snippets, to be used as an introduction to the use of unicode. This is, of course, an on-going thing, and hopefully additional snippets and comments will be forthcoming from the members.

[code]# snippet number 1

hebrewword = u'\u05e9\u05dc\u05d5\u05dd'     # assign  the unicode characters to 'hebrewword'
print "(Output of snippet number 1)"
print hebrewword                                                    # print 'hebrewword' which is the Hebrew characters for "shalom"'
print " --------------------------------------------------------------------------------------------------------------"
print
# snippet number 2
# Code snippet using unicode and English combined in one sentence

hebrew_peace = unicode (u'\u05e9\u05dc\u05d5\u05dd')        # assign the  unicode characters to hebrew_peace
print "(Output of snippet number 2)"
print "The word 'peace' in Hebrew  is , " + hebrew_peace , ", pronounced 'shalom'."  # output the combined English/Hebrew sentence

print "-------------------------------------------------------------------------------------------------------------"
print
# snippet number 3
# Define a unicode function

def peace():                        # define the function
    print  u'\u05e9\u05dc\u05d5\u05dd'   #  'This, again is the unicode for the Hebrew word, shalom.' 

word = peace                      # assign the function as an object
print "(Output of snippet number 3)"
word()                                  # call the funtion by its new name
    

print "-----------------------------------------------------------------------------------------------------------"
print
# snippet number 4
# Not yet able to use a unicode character in a tuple, dictionary or list
# Still working on it
shalom = u'\u05e9\u05dc\u05d5\u05dd'
print "(First output of snippet number 4)"
print shalom         # indicates that the unicode characters have been properly assigned to 'shalom'
print
myTuple = (shalom,"peace")    # attempt to assign the object 'shalom' and the word 'peace' to a tuple

print "(Second output of snippet number 4, showing failed attempt)"
print myTuple        # printout is of the unicode characters rather than the Hebrew word.
print "------------------------------------------------------------------------------------------------------------"
# snippet number 5
print " (First output of snippet number 5)"
a = u'\u05E9'
print a
b = u'\u05DC'
print b
c = u'\u05D5'
print c
d = u'\u05DD'
print d
print 
all = a+b+c+d +"= peace"
print " (Second output of snippet number 5)"
print all
print 
e =[a+b+c+d]                     # attempt to assign the unicode characters to a list
print " (Third output of snippet number 5, showing failed attempt)"
print e                                 # as stated in snippet  4 above, can't do it at this time
print 
print " (Fourth output of snippet number 5)"
print a+b+c+d
print
a = u'\u05e9\u05dc\u05d5\u05dd'
print " (Fifth output of snippet number 5)"
print a
print "------------------------------------------------------------------------------------------------------------"



[/code]

(Output of snippet number 1)
שלו×?
 --------------------------------------------------------------------------------------------------------------

(Output of snippet number 2)
The word 'peace' in Hebrew  is , שלו×? , pronounced 'shalom'.
-------------------------------------------------------------------------------------------------------------

(Output of snippet number 3)
שלו×?
-----------------------------------------------------------------------------------------------------------

(First output of snippet number 4)
שלו×?

(Second output of snippet number 4, showing failed attempt)
(u'\u05e9\u05dc\u05d5\u05dd', 'peace')
------------------------------------------------------------------------------------------------------------
 (First output of snippet number 5)
ש
ל
ו
×?

 (Second output of snippet number 5)
שלו×?= peace

 (Third output of snippet number 5, showing failed attempt)
[u'\u05e9\u05dc\u05d5\u05dd']

 (Fourth output of snippet number 5)
שלו×?

 (Fifth output of snippet number 5)
שלו×?
------------------------------------------------------------------------------------------------------------
>>> [/code]
iacobus 0 Newbie Poster

I'm sorry the Hebrew characters did not print. I don't know how to correct this, at this time.
Iacobus

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Don't feel too bad. I don't think the DaniWeb code-field handles foreign characters. I had trouble with spanish characters too, and they are simple ASCII. Interesting project though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.