-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path002_strings.py
261 lines (187 loc) · 3.92 KB
/
002_strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.19"
app = marimo.App(width="medium")
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# 🎭 Strings
This notebook introduces **strings**, which are containers for text.
## Creating strings
Create strings by wrapping text in quotes:
```python
# Use double quotes
greeting = "Hello, Python!"
# or single quotes
name = 'Alice'
# or triple quotes
multiline_string = \"""
Dear, Alice,
Nice to meet you.
Sincerely,
Bob.
\"""
```
Below is an example string.
"""
)
return
@app.cell
def _():
text = "Python is amazing!"
text
return (text,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Essential string operations
Here are some methods for working with strings.
Tip: Try changing the value of `text` above, and watch how the
computed values below change.
"""
)
return
@app.cell
def _(text):
# the `len` method returns the number of characters in the string.
len(text)
return
@app.cell
def _(text):
text.upper()
return
@app.cell
def _(text):
text.lower()
return
@app.cell
def _(text):
text.title()
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""Use string methods and the `in` operator to find things in strings.""")
return
@app.cell
def _(text):
# Returns the index of "is" in the string
text.find("is")
return
@app.cell
def _(text):
"Python" in text
return
@app.cell
def _(text):
"Javascript" in text
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Inserting values in strings
Modern Python uses f-strings to insert values into strings. For example,
check out how the next cell greets you (and notice the `f''''`)!
**Try it!** Enter your name in `my_name` below, then run the cell.
"""
)
return
@app.cell
def _():
my_name = ""
return (my_name,)
@app.cell
def _(my_name):
f"Hello, {my_name}!"
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Working with parts of strings
You can access any part of a string using its position (index):
"""
)
return
@app.cell
def _(text):
first_letter = text[0]
first_letter
return (first_letter,)
@app.cell
def _(text):
last_letter = text[-1]
last_letter
return (last_letter,)
@app.cell
def _(text):
first_three = text[0:3]
first_three
return (first_three,)
@app.cell
def _(text):
last_two = text[-2:]
last_two
return (last_two,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Other helpful string methods
Finally, here are some other helpful string methods. Feel free to try them out on your own strings by modifying the value of `sentence` below.
"""
)
return
@app.cell
def _():
sentence = " python is fun "
sentence
return (sentence,)
@app.cell
def _(sentence):
# Remove extra spaces
sentence.strip()
return
@app.cell
def _(sentence):
# Split into a list of words
sentence.split()
return
@app.cell
def _(sentence):
sentence.replace("fun", "awesome")
return
@app.cell
def _():
"123".isdigit(), "abc".isdigit()
return
@app.cell
def _():
"123".isalpha(), "abc".isalpha()
return
@app.cell
def _():
"Python3".isalnum()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Next steps
For a full primer on strings, check out the [official documentation](https://docs.python.org/3/library/string.html).
"""
)
return
@app.cell
def _():
import marimo as mo
return (mo,)
if __name__ == "__main__":
app.run()