1
message = str(input())
for i in message:
  if i == "a": i = 1
  if i == "b": i = 2
  print(i)

ect. I am trying to create a code generator where the user inputs a string eg. "hello" and it is converted to a number code by assigning each letter a number, based on their position in the alphabet. 1 = a, 2 = b and so on. The method I am currently using is very long - are there other ways to do this to avoid this issues?

How can I print the answer together, without the numbers being on multiple lines eg. 1 2 3 21 19

  • Could you please be specific as to which problem are you trying to solve here? It is unclear what is what is your input and expected output. – norok2 Feb 28 '21 at 12:03
  • @norok2 Each letter that the user inputs should be converted to a number. a = 1, b = 2, ect. So if the user inputs abc, the output should be 123. –  Feb 28 '21 at 12:06
  • @FranciscaRiosDurkin I helps to add that input/output example to your question. – aneroid Feb 28 '21 at 12:28
  • Letters cardinality is larger than number's one. How is the difference between 'k' and 'aa'. They would both get you to '11' – norok2 Feb 28 '21 at 13:51
  • @norok2 Each output number prints on a new line, so whereas k would have 11 on one line, aa would have 1 then a line break, then another 1. –  Mar 01 '21 at 08:02
  • I thought you wanted to have it all in one line without spaces – norok2 Mar 01 '21 at 09:02

5 Answers5

2

Use string lib:

import string
[string.ascii_lowercase.index(s)+1 for s in 'hello'.lower()]

Output:

[8, 5, 12, 12, 15]
Alderven
  • 7,569
  • 5
  • 26
  • 38
1

You could convert the letter to its ASCII code value, calculate its position, then add the position of the letter to the string and print the string. To get the numeric value of a char, use the ord() method, to get a character from a numeric value, use the char() method.

Link to ASCII table

Unspoiled9710
  • 102
  • 2
  • 11
1
import string

low_letter = list(string.ascii_lowercase)

now you have a list of all letters in order

so...

message = str(input())
for i in message:
    print(low_letter.index(i))

now you have the index

and in case you need the upper case :

upper_case = list(string.ascii_uppercase)
Robert.b
  • 53
  • 1
  • 6
1
```
msg = str(input())
for char in msg:
    print(ord(char) - ord('a') + 1)

The idea is to convert each character into ASCII using ord() in python and subtract it with the ASCII of 'a' and + 1
So consider "hello" string : 
The output will be :
hello
8
5
12
12
15



Note : Here we subtract the ASCII of the character with the ASCII of character 'a' in order to get the correct Position as in Alphabetical order.
Eg : h 
Ascii of h = 104
Ascii of a = 97
So our required answer = Ascii of h - Ascii of a + 1
                       = 104 - 97 + 1 = 8

And if we look the alphabetical order - a,b,c,d,e,f,g,h -> h is the 8th character
Hope this helps you. Thank you 
Rohith V
  • 1,089
  • 1
  • 9
  • 23
0

Just create a dictionary that maps 'a' to 1, 'b' to 2, etc

from string import ascii_lowercase
mapping = dict(zip(ascii_lowercase, range(1, 27)))

And use it like this

numbers = [mapping[char] for char in message if char in mapping]

You need to check if each character is in the alphabet, otherwise you'll get an error. If you also want to cover the uppercase letters, change message to message.lower() in the loop line.

Using index() every iteration is inefficient, even though in this case it won't matter much because you're searching through at most 26 letters. But in general it's not a good strategy. Dictionary lookup is O(1).

Reti43
  • 9,656
  • 3
  • 28
  • 44