-2

I need to build a mini program that the user types something in the input and the output will be the alphabet without the letters that the user put in the input. So i wrote this:

alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
text = input("Please enter any text")
for character in text:
    if character in alphabet:
        alphabet.remove(character)
print(alphabet)

But now I need to build the variable alphabet as an array, using ascii, without having to assign the letters manually, how do I do it?

001
  • 13,291
  • 5
  • 35
  • 66
  • 3
    One option is to use [`chr`](https://docs.python.org/3/library/functions.html#chr) and [`ord`](https://docs.python.org/3/library/functions.html#ord): `[chr(i) for i in range(ord('a'), ord('z')+1)]` – 001 Jan 03 '22 at 16:27
  • 2
    `import string; alphabet = list(string.ascii_lowercase)` – kindall Jan 03 '22 at 16:28
  • Does this answer your question? [Alphabet range in Python](https://stackoverflow.com/q/16060899/6045800) – Tomerikoo Jan 03 '22 at 16:31
  • Or [Is There an Already Made Alphabet List In Python?](https://stackoverflow.com/q/58960689/6045800) or even [Python: how to print range a-z?](https://stackoverflow.com/q/3190122/6045800) – Tomerikoo Jan 03 '22 at 16:32

1 Answers1

1

Try this:

from string import ascii_lowercase

text = set(input()) # text = input() is also fine
print([c for c in ascii_lowercase if c not in text])

Since text can be quite long, it's better to convert it into a set before starting using if c not in text. This is completely optional though if you don't care about time complexity.

Another option (here you use the fact that each letter can be converted to a character by using ord, and consecutive letters map to consecutive numbers):

text = set(input()) # text = input() is also fine
print([chr(i) for i in range(ord('a'), ord('z') + 1) if chr(i) not in text])

One more option (difference between two sets, followed by a sorting operation):

from string import ascii_lowercase

text = input()
print(sorted(set(ascii_lowercase) - set(text)))
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50