Python Forum
Simple question help me plz
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question help me plz
#1
Hello. If I have a string: 'ABC', how can I give each character a value so it can be coded?

ex: A = 1
     B = 2
     C = 3

input:

x = 'ABC'

output:

y = '123'

im new in python, maybe its an easy one but i cant find a way to do this. thanks for your attention
Reply
#2
Why would you want to do that? Please explain your end goal. Thank you.
Reply
#3
(Oct-28-2016, 07:06 PM)Kebap Wrote: Why would you want to do that? Please explain your end goal. Thank you.

I want to code a message. Its just a begginner's exercise of python that i found outhere. I dont know where and how to start the writing of this code. Can you help me?
Reply
#4
Start with the first line

hint - strings can be encoded, decoded with 'encode', and 'decode' commands
characters can be converted using ord and chr

suggestion - try each and see which one you need
Reply
#5
You don't want to do that.  Variable variable names are not a good thing.  While possible, the solution will lead you down the path of the dark side.
Try a dictionary instead, it can do pretty much anything you'd want variable variables for.
>>> items = { 'A': 1, 'B': 2, 'C': 3 }
>>> keys = 'ABC'
>>> collected = []
>>> for key in keys:
...   collected.append(items[key])
...
>>> collected
[1, 2, 3]
>>> print(''.join(map(str, collected)))
123
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Question for Most - Help Explain Please emerger 8 3,874 Jan-27-2018, 10:03 PM
Last Post: emerger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020