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.
* CodingPython

Avatar of undefined
Last Comment
gelonida

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Norie

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jake

ASKER
Thank you this was very helpful!
gelonida

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
gelonida

@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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Norie

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

@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.
Norie

Oops, missed that - apologies.:)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gelonida

@Norie:
edited my previous answer