Skip to content

Commit cf50a38

Browse files
authored
[Tutorial] String Reversal Techniques (inspirezonetech#142)
* I added two ways of reversing strings , but I was contemplating on adding the deque method but thought it was too complex * fixed the flake8 errors
1 parent 3cee4c7 commit cf50a38

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

string-formatting/string-reversal.py

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
# ------------------------------------------------------------------------------------
2-
# Tutorial: brief description of tutorial content
2+
# We're going to look at a few different ways that you can reverse the structure of a string .
3+
# Let's begin.
34
# ------------------------------------------------------------------------------------
45

5-
# Code here explaining concept with comments to guide
6+
# Single Words , A string is an iterable and anything within these --> ""
7+
# 1. The built-in reverse method
8+
# This method is used on iterables , commonly on lists and strings
9+
# Example of functions that reverse a string.
10+
def reverseString(word):
11+
reversedWord = "".join(reversed(word))
12+
return reversedWord
13+
'''
14+
Let's break down what's above:
15+
Remember that all strings are immutable but iterable. So if that's the case , we can't use the .reverse() function like we could on a list.
16+
So we iterate through each letter and use the reversed() function . Now since the reversed() function return an iterable;a list .
17+
We use the join() to concatenate the letters and then we return the reversed String.
18+
'''
19+
# 2.The reverse slice method
20+
# This method also works on iterables too.
21+
# Let's use the same example with this method too
22+
def backString(word):
23+
reversedWord = word[::-1]
24+
return reversedWord
25+
'''
26+
Let's break this down as well:
27+
The slicing technique is used to get a certain portion of string and using the starting and ending index values.
28+
e.g [a:b], this slices from position to position before b ; so b-1 . What we don't commonly use is the third option of [a:b:c] <-- C.
29+
What c does is specify the number of characters to jump or skip over . So if c == 2 , we're gonna skip over 2 characters .But if c == -1,this specifies that the string begins from the back.
30+
We commonly use -1 when we want the last letter or character in a string e.g word = "pet",word[-1] == t.
31+
Back to the function , if we don't specify the starting and end values , the computer just assumes it's the whole string .
32+
So e.g [a:] this is from a to the last , [:b] from the first to b , [a::c] from a to the last every c characters , [:b:c] from the first to b every c characters .
33+
So [::-1] means from the first to last in reverse .
34+
'''
635

736
# ------------------------------------------------------------------------------------
8-
# Challenge: list challenges to be completed here. minimum of one challenge per tutorial
9-
# ------------------------------------------------------------------------------------
37+
# The format is the same even for sentences : So write a function that reverses the sentence "I love ketchup".

0 commit comments

Comments
 (0)