John Beats
John Beats

Reputation: 69

All possible combinations add of letters

I greet you all I need help I have this code here

how can i achieve this if I want letters to be added between the letters

example
input
east

output
aeast
eaast
eaast
easat
easta
aeast

beast
ebast
eabst
easbt
eastb

ect...

leters = 'abcdefghijklmnopqrstuvwxyz'


words = ('east')


for letersinput in leters:
    for rang in range(1):
        print(letersinput+""+str(words)+"")

I'm looking for the exact opposite of this, how to do it?

        def missing_char(word, n):
          n = abs(n)
          front = word[:n]
          back = word[n+1:]
          return front + back

Upvotes: 0

Views: 87

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177901

Iterate through the words and letters, and for each position, slice the word at the position and insert the letter:

letters = 'abcdefghijklmnopqrstuvwxyz'

words = ['east']

for word in words:
    for letter in letters:
        for i in range(len(word)+1):
            print(word[:i] + letter + word[i:])
        print()

Output:

aeast
eaast
eaast
easat
easta

beast
ebast
eabst
easbt
eastb

...

zeast
ezast
eazst
easzt
eastz

Upvotes: 1

Related Questions