Last active
August 8, 2016 16:50
-
-
Save brainyfarm/1a7966c1614fe429b986a7683e5be74e to your computer and use it in GitHub Desktop.
FreeCodeCamp - Reverse a String (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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