How to reverse and manipulate a string in python [duplicate]

I am creating a translator in python . This translator is going to change a normal text to a text with some special things :

  1. At the first of each word we add "S"
  2. At the End of each word we add "Di"
  3. We reverse each word

example : Hello Everyone --> SHello SEveryone --> SHelloDi SEveryoneDi --> iDolleHS iDenoyrevES

I did first two parts easily; but third part is a little tricky my code :

n = input("Enter Text : ")
y = n.split()
z = 0

for i in y:
    x = str("S" + i)
    y[z] = x
    z = z + 1

z = 0

for i in y:
    x = str(i + "Di")
    y[z] = x
    z = z + 1

print(y)

z = 1

for i in y:
    globals()["x%s" % z] = []
    for j in i:
        pass

In pass part I wanna to do something like this x{i}.append(j) and then we reverse it.

How do I do this?


Solution 1:

You can reverse it using ::-1, it means from start to beginning in reverse order:
For example:

print("abcd"[::-1]) # will prin dcba

So the code for every word can look like this:

result = "S"+word+"Di"
result = result[::-1]

Now you just have to put that in a loop and do it for every word.