Python daily exercise 3: reverse a string

mathewchan

HHMathewChan

Posted on June 4, 2022

Python daily exercise 3: reverse a string

Question

  • write a program to reverse a given string
str1 = "PYnative"

Enter fullscreen mode Exit fullscreen mode
  • Expected output
evitanYP
Enter fullscreen mode Exit fullscreen mode

My attempt

str1 = "PYnative"
reversed_string = str1[::-1]
print(reversed_string)
Enter fullscreen mode Exit fullscreen mode

Recommend solution

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

str1 = ''.join(reversed(str1))
print("Reversed String is:", str1)
Enter fullscreen mode Exit fullscreen mode

My reflection

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

💖 💪 🙅 🚩
mathewchan
HHMathewChan

Posted on June 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related