Question
- write a program to reverse a given string
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().
Top comments (2)
Looks like using slice is 4x faster. Timed with
%timeit
command ofYPython
.just find out your comment, good to know how to count program time, thx