Python beginner exercise 3: reverse a string

Table of Contents
str1 = "PYnative"
  • Expected output
    evitanYP

My attempt

str1 = "PYnative"
reversed_string = str1[::-1]
print(reversed_string)

Recommend solution

str1 = "PYnative"
print("Original String is:", str1)

str1 = ''.join(reversed(str1))
print("Reversed String is:", str1)

My reflection

I only know how to reversed a string using string slicing method, but do not know the reverse() and the join().

Credit

Exerice on PYnative

Leave a Reply