1

I just want to know if this is a good way or not.

Input_str = "random Str"
Output_str = ""

for i in range(len(Input_str)+1):
      if i == 0: continue
      Output_str += Input_str[-i]

print(Output_str)
LeanderC
  • 11
  • 1

2 Answers2

2

To quickly reverse a string use the following code:

string = "string"
string[::-1]
>>> gnirts

It takes the entire string and iterates reversely over it .

1

There is a better and more pythonistic way to do this (although your code works just fine).

output_str = input_str[::-1]

This will pretty much do exactly what your code does - it will iterate from the end of the string towards the front and save that reversed string into the new variable.

You can read more about this syntax called string slicing here.

Filip Müller
  • 1,096
  • 5
  • 18