Link to home
Start Free TrialLog in
Avatar of Jake
Jake

asked on

Need help with simply python code

Am new to python, and am struggling to combine bits of theory I have learnt together, For Loops, Ord() and Appending.

I am looking to create a for loop and print a string of unicode numbers for a word.
For example:
word=aaaaa
I want it to print
[97, 97, 97, 97, 97]
where 97 is the unicode number for a.
im currently working with

word=aaaaa
unicode=[]
for ___ in range(len(word)):
       unicode.append(____)
print(unicode)

my main struggle is filling out these blanks. ive played around with ord() and char, but cant seem to get the function to print it successfully but
any help would be appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jake
Jake

ASKER

Thank you this was very helpful!
Just for info:

Using an integer as loop variable and indexing into an iterable is not the most typical way one would write code in Python.

So the answer, that you got is working and is the answer that tries to keep as much of your original code as possible.

What you would probably see more often would be either Norie's second solution, that's using a list expression or something like

word=u'aaaaa'
unicode=[]
for char in word:  # you can directly iterate through a string character by character
       unicode.append(ord(char))
print(unicode)

Open in new window


Also if you knew, that your will never run with python2, then you can simply write:
word= 'aaaaa'  # the leading u is only required for python2

Open in new window


If on the other hand you want your code to run with Python2, then I would rename the variable unicode into something else (.e.g. unicode_codes ), as unicode is a reserved word in python2 and you would overload it, which might be a problem depending on the context
@Norie: There is a tiny typo in your second example, which would make the code fail if it would be copied pasted.
Once you write unicode and the other time you write Unicode
gelonida

Wouldn't a list comprehension as I offered for an alternative solution be more 'pythonic'?

P.S. I was posting from my phone and it auto-corrected 'unicode' to 'Unicode', I didn't notice until after the solution was accepted and wasn't able to edit.:)
@Norie


Yes indeed. That's why I wrote

What you would probably see more often would be either Norie's second solution .. or

Perhaps you were reading too fast.  (Meaning before my final save/correction) I think my first version might not have contained the mention of your second solution.
I think I made an intermediate save and saw only afterwards your second snippet.

However sometimes list expressions are too complicated for beginners and sometimes you have code, that is too complicated for or impossible to do with list expressions.



I think it is good to know, that
for elem in iterable:
   dosomethingwith(elem)

Open in new window


prefered to:
for i in range(len(iterable)):
       dosomethingwith(iterable[i])

Open in new window



Also. Yes I know these mobile phones can be annoying with their automatically adapting cases when writing code snippets.

Sometimes I'd wish one would be allowed to propose typo and spelling fixes after an answer got accepted.
Oops, missed that - apologies.:)
@Norie:
edited my previous answer