-2

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?

Joundill
  • 6,828
  • 12
  • 36
  • 50
Peyman S87
  • 53
  • 4
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/284236. To post here, you should identify a *specific problem* and ask a question that is about the problem *that you need help with*, not the overall problem that your program is trying to solve. In this case, the important part is how to reverse a string. You can find that kind of information easily with a search engine, which would probably find you (for example) the existing duplicate question. – Karl Knechtel Jan 23 '22 at 11:47

1 Answers1

2

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
S H
  • 415
  • 3
  • 14