Skip to content

Commit cb300ed

Browse files
authored
[Little Sister's Vocab]: Fixed up Code Examples for str.join() & Added an Additional Hint. (#3995)
* Fixed up code examples for join and added an additional hint. * Touched up hints phrasing, added no loop directive to instructions, and added additional examples to concept about. * Typo correction. * Corrected separator misspelling. * Cleaned up in-line comments per PR review. * Fixed capitalization on inline comments in last join example.
1 parent fbc6769 commit cb300ed

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

concepts/strings/about.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The Python docs also provide a very detailed [unicode HOWTO][unicode how-to] tha
99
Strings implement all [common sequence operations][common sequence operations] and can be iterated through using `for item in <str>` or `for index, item in enumerate(<str>)` syntax.
1010
Individual code points (_strings of length 1_) can be referenced by `0-based index` number from the left, or `-1-based index` number from the right.
1111

12-
Strings can be concatenated with `+`, or via `<str>.join(<iterable>)`, split via `<str>.split(<separator>)`, and offer multiple formatting and assembly options.
12+
Strings can be concatenated with `+`, or via `<str>.join(<iterable>)`, split via `<str>.split(<separator>)`, and offer multiple formatting, assembly, and templating options.
1313

1414

1515
A `str` literal can be declared via single `'` or double `"` quotes. The escape `\` character is available as needed.
@@ -168,12 +168,12 @@ sentence = word + " " + "means" + " " + number + " in " + language + "."
168168
"дев'ять means nine in Ukrainian."
169169
```
170170

171-
If a `list`, `tuple`, `set` or other collection of individual strings needs to be combined into a single `str`, [`<str>.join(<iterable>)`][str-join], is a better option:
171+
If a `list`, `tuple`, `set` or other collection of individual strings needs to be combined into a single `str`, [`<str>.join(<iterable>)`][str-join] is a better option:
172172

173173

174174
```python
175175
# str.join() makes a new string from the iterables elements.
176-
>>> chickens = ["hen", "egg", "rooster"]
176+
>>> chickens = ["hen", "egg", "rooster"] # Lists are iterable.
177177
>>> ' '.join(chickens)
178178
'hen egg rooster'
179179

@@ -183,6 +183,34 @@ If a `list`, `tuple`, `set` or other collection of individual strings needs to b
183183

184184
>>> ' 🌿 '.join(chickens)
185185
'hen 🌿 egg 🌿 rooster'
186+
187+
188+
# Any iterable can be used as input.
189+
>>> flowers = ("rose", "daisy", "carnation") # Tuples are iterable.
190+
>>> '*-*'.join(flowers)
191+
'rose*-*daisy*-*carnation'
192+
193+
>>> flowers = {"rose", "daisy", "carnation"} # Sets are iterable, but output order is not guaranteed.
194+
>>> '*-*'.join(flowers)
195+
'rose*-*carnation*-*daisy'
196+
197+
>>> phrase = "This is my string" # Strings are iterable, but be careful!
198+
>>> '..'.join(phrase)
199+
'T..h..i..s.. ..i..s.. ..m..y.. ..s..t..r..i..n..g'
200+
201+
202+
# Separators are inserted **between** elements, but can be any string (including spaces).
203+
# This can be exploited for interesting effects.
204+
>>> under_words = ['under', 'current', 'sea', 'pin', 'dog', 'lay']
205+
>>> separator = ' ⤴️ under' # Note the leading space, but no trailing space.
206+
>>> separator.join(under_words)
207+
'under ⤴️ undercurrent ⤴️ undersea ⤴️ underpin ⤴️ underdog ⤴️ underlay'
208+
209+
# The separator can be composed different ways, as long as the result is a string.
210+
>>> upper_words = ['upper', 'crust', 'case', 'classmen', 'most', 'cut']
211+
>>> separator = ' 🌟 ' + upper_words[0] # This becomes one string, similar to ' ⤴️ under'.
212+
>>> separator.join(upper_words)
213+
'upper 🌟 uppercrust 🌟 uppercase 🌟 upperclassmen 🌟 uppermost 🌟 uppercut'
186214
```
187215

188216
Strings support all [common sequence operations][common sequence operations].
@@ -194,7 +222,9 @@ Indexes _with_ items can be iterated through in a loop via `for index, item in e
194222

195223
>>> exercise = 'လေ့ကျင့်'
196224

197-
# Note that there are more code points than perceived glyphs or characters
225+
# Note that there are more code points than perceived glyphs or characters.
226+
# Care should be used when iterating over languages that use
227+
# combining characters, or when dealing with emoji.
198228
>>> for code_point in exercise:
199229
... print(code_point)
200230
...

exercises/concept/little-sisters-vocab/.docs/hints.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ There's four activities in the assignment, each with a set of text or words to w
1414

1515
## 2. Add prefixes to word groups
1616

17-
- Believe it or not, [`str.join()`][str-join] is all you need here.
18-
- Like [`str.split()`][str-split]`, `str.join()` can take an arbitrary-length string, made up of any unicode code points.
17+
- Believe it or not, [`str.join()`][str-join] is all you need here. **A loop is not required**.
18+
- The tests will be feeding your function a `list`. There will be no need to alter this `list` if you can figure out a good delimiter string.
19+
- Remember that delimiter strings go between elements and "glue" them together into a single string. Delimiters are inserted _without_ space, although you can include space characters within them.
20+
- Like [`str.split()`][str-split], `str.join()` can process an arbitrary-length string, made up of any unicode code points. _Unlike_ `str.split()`, it can also process arbitrary-length iterables like `list`, `tuple`, and `set`.
21+
1922

2023
## 3. Remove a suffix from a word
2124

exercises/concept/little-sisters-vocab/.docs/instructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Implement the `make_word_groups(<vocab_words>)` function that takes a `vocab_wor
4040
`[<prefix>, <word_1>, <word_2> .... <word_n>]`, and returns a string with the prefix applied to each word that looks like:
4141
`'<prefix> :: <prefix><word_1> :: <prefix><word_2> :: <prefix><word_n>'`.
4242

43+
Creating a `for` or `while` loop to process the input is not needed here.
44+
Think carefully about which string methods (and delimiters) you could use instead.
45+
4346

4447
```python
4548
>>> make_word_groups(['en', 'close', 'joy', 'lighten'])

exercises/concept/little-sisters-vocab/.docs/introduction.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ If a `list`, `tuple`, `set` or other collection of individual strings needs to b
5050

5151
```python
5252
# str.join() makes a new string from the iterables elements.
53-
>>> chickens = ["hen", "egg", "rooster"]
53+
>>> chickens = ["hen", "egg", "rooster"] # Lists are iterable.
5454
>>> ' '.join(chickens)
5555
'hen egg rooster'
5656

@@ -60,6 +60,34 @@ If a `list`, `tuple`, `set` or other collection of individual strings needs to b
6060

6161
>>> ' 🌿 '.join(chickens)
6262
'hen 🌿 egg 🌿 rooster'
63+
64+
65+
# Any iterable can be used as input.
66+
>>> flowers = ("rose", "daisy", "carnation") # Tuples are iterable.
67+
>>> '*-*'.join(flowers)
68+
'rose*-*daisy*-*carnation'
69+
70+
>>> flowers = {"rose", "daisy", "carnation"} # Sets are iterable, but output order is not guaranteed.
71+
>>> '*-*'.join(flowers)
72+
'rose*-*carnation*-*daisy'
73+
74+
>>> phrase = "This is my string" # Strings are iterable, but be careful!
75+
>>> '..'.join(phrase)
76+
'T..h..i..s.. ..i..s.. ..m..y.. ..s..t..r..i..n..g'
77+
78+
79+
# Separators are inserted **between** elements, but can be any string (including spaces).
80+
# This can be exploited for interesting effects.
81+
>>> under_words = ['under', 'current', 'sea', 'pin', 'dog', 'lay']
82+
>>> separator = ' ⤴️ under'
83+
>>> separator.join(under_words)
84+
'under ⤴️ undercurrent ⤴️ undersea ⤴️ underpin ⤴️ underdog ⤴️ underlay'
85+
86+
# The separator can be composed different ways, as long as the result is a string.
87+
>>> upper_words = ['upper', 'crust', 'case', 'classmen', 'most', 'cut']
88+
>>> separator = ' 🌟 ' + upper_words[0]
89+
>>> separator.join(upper_words)
90+
'upper 🌟 uppercrust 🌟 uppercase 🌟 upperclassmen 🌟 uppermost 🌟 uppercut'
6391
```
6492

6593
Code points within a `str` can be referenced by `0-based index` number from the left:
@@ -95,7 +123,6 @@ creative = '창의적인'
95123

96124
```
97125

98-
99126
There is no separate “character” or "rune" type in Python, so indexing a string produces a new `str` of length 1:
100127

101128

@@ -169,7 +196,6 @@ Strings can also be broken into smaller strings via [`<str>.split(<separator>)`]
169196
['feline', 'four-footed', 'ferocious', 'furry']
170197
```
171198

172-
173199
Separators for `<str>.split()` can be more than one character.
174200
The **whole string** is used for split matching.
175201

0 commit comments

Comments
 (0)