|
1 | 1 | # ------------------------------------------------------------------------------------
|
2 | 2 | # We're going to look at a few different ways that you can reverse the structure of a string .
|
3 |
| -# Let's begin |
| 3 | +# Let's begin. |
4 | 4 | # ------------------------------------------------------------------------------------
|
5 | 5 |
|
6 | 6 | # Single Words , A string is an iterable and anything within these --> ""
|
7 | 7 | # 1. The built-in reverse method
|
8 | 8 | # This method is used on iterables , commonly on lists and strings
|
9 |
| -# Example of a function that reverses a string |
| 9 | +# Example of functions that reverse a string. |
10 | 10 | def reverseString(word):
|
11 | 11 | reversedWord = "".join(reversed(word))
|
12 | 12 | return reversedWord
|
13 | 13 | '''
|
14 | 14 | Let's break down what's above:
|
15 | 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 | 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 |
| 17 | +We use the join() to concatenate the letters and then we return the reversed String. |
18 | 18 | '''
|
19 |
| -#2. The reverse slice method |
| 19 | +# 2.The reverse slice method |
20 | 20 | # This method also works on iterables too.
|
21 | 21 | # Let's use the same example with this method too
|
22 |
| -def reverseString(word): |
| 22 | +def backString(word): |
23 | 23 | reversedWord = word[::-1]
|
24 | 24 | return reversedWord
|
25 | 25 | '''
|
26 | 26 | Let's break this down as well:
|
27 | 27 | The slicing technique is used to get a certain portion of string and using the starting and ending index values.
|
28 | 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 . |
30 |
| -But if c == -1 , this specifies that the string begins from the back . |
31 |
| -We commonly use -1 when we want the last letter or character in a string e.g word = "pet" , word[-1] == t. |
| 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. |
32 | 31 | Back to the function , if we don't specify the starting and end values , the computer just assumes it's the whole string .
|
33 | 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 .
|
34 | 33 | So [::-1] means from the first to last in reverse .
|
35 | 34 | '''
|
36 | 35 |
|
37 |
| - |
38 |
| - |
39 | 36 | # ------------------------------------------------------------------------------------
|
40 | 37 | # The format is the same even for sentences : So write a function that reverses the sentence "I love ketchup".
|
41 |
| -# ------------------------------------------------------------------------------------ |
0 commit comments