Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Last active August 8, 2016 16:50
Show Gist options
  • Save brainyfarm/1a7966c1614fe429b986a7683e5be74e to your computer and use it in GitHub Desktop.
Save brainyfarm/1a7966c1614fe429b986a7683e5be74e to your computer and use it in GitHub Desktop.
FreeCodeCamp - Reverse a String (Python)
"""
Reverse the provided string.
Your result must be a string.
"""
def reverse_string(string):
# Turn the string into a list
string_list = list(string)
# Reverse the list
string_list.reverse()
# Convert list to string and return
return "".join(string_list)
print reverse_string("hello")
# This version does the job using string slicing with the power of striding
# A very fast one as well :)
def reverse_string(string):
return string[-1::-1]
print reverse_string("hello")
# This version does the job using string slicing with the power of striding (-1)
# A very fast one as well :)
def reverse_string(string):
return string[::-1]
print reverse_string("hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment