@@ -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,8 +60,37 @@ 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 seperator 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
93+
6594Code points within a ` str ` can be referenced by ` 0-based index ` number from the left:
6695
6796
0 commit comments