
Jesse S. answered 03/29/19
Enthusiastic Python Tutor with 96% Passing Score
Hi there!
One way to reverse a string is to use a "trick" that implements string slicing. It takes very few lines of code and is very quick and easy. However, I have read from Dan Bader at DBader.org that this method of string reversal isn't the most recommended, even though it is the easiest and quickest. And the reason for that is because it isn't easy for the "uninitiated" to understand what's happening when reading the code...in other words, it isn't an intuitive way of expressing what's going on. Nevertheless, if you're simply wanting to just get the job done, then here you go!
- define a function which takes a string as an argument
- the function will return a reversed copy of the string using slicing
- when the function is called with a string (such as "hello world") passed into it as an argument, the function will perform and a reversed copy will be returned