Link to home
Create AccountLog in
Avatar of 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:
I want it to print
[97, 97, 97, 97, 97]
where 97 is the unicode number for a.
im currently working with

for ___ in range(len(word)):

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.
Avatar of Norie

Link to home
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Jake


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

for char in word:  # you can directly iterate through a string character by character

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

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.:)

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:

Open in new window

prefered to:
for i in range(len(iterable)):

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.:)
edited my previous answer