Spaces:
Running
Running
Akshay Agrawal
commited on
Commit
·
e0a5cdf
1
Parent(s):
6cf1710
Updates: split markdown into code cells
Browse files
Python/phase_1/string_manipulation.py
CHANGED
@@ -7,69 +7,182 @@
|
|
7 |
|
8 |
import marimo
|
9 |
|
10 |
-
__generated_with = "0.10.
|
11 |
app = marimo.App()
|
12 |
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
@app.cell(hide_code=True)
|
15 |
def _(mo):
|
16 |
mo.md(
|
17 |
"""
|
18 |
-
# 🎭
|
|
|
|
|
19 |
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
## Creating Your First Strings
|
23 |
-
In Python, strings are like containers for text. You can create them in two simple ways:
|
24 |
```python
|
25 |
greeting = "Hello, Python!" # using double quotes
|
26 |
-
name = 'Alice'
|
27 |
```
|
28 |
|
29 |
-
|
30 |
-
Let us explore what you can do with strings:
|
31 |
-
```python
|
32 |
-
text = "Python is amazing!"
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
print(text.lower()) # python is amazing!
|
38 |
-
print(text.title()) # Python Is Amazing!
|
39 |
|
40 |
-
# Finding things in strings
|
41 |
-
print(text.find('is')) # Find where 'is' starts: 7
|
42 |
-
print('Python' in text) # Check if 'Python' exists: True
|
43 |
-
```
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
"""
|
53 |
)
|
54 |
return
|
55 |
|
56 |
|
57 |
@app.cell
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
def _(mo):
|
59 |
mo.md(
|
60 |
"""
|
61 |
## Working with Parts of Strings
|
62 |
You can access any part of a string using its position (index):
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
-
first_letter = text[0] # 'P'
|
67 |
-
last_letter = text[-1] # 'n'
|
68 |
-
first_three = text[0:3] # 'Pyt'
|
69 |
-
last_two = text[-2:] # 'on'
|
70 |
-
```
|
71 |
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
```python
|
74 |
sentence = " python is fun "
|
75 |
|
@@ -92,7 +205,7 @@ def _(mo):
|
|
92 |
return
|
93 |
|
94 |
|
95 |
-
@app.cell
|
96 |
def _(mo):
|
97 |
callout_text = mo.md("""
|
98 |
## Your String Journey Begins!
|
@@ -112,11 +225,5 @@ def _(mo):
|
|
112 |
return (callout_text,)
|
113 |
|
114 |
|
115 |
-
@app.cell
|
116 |
-
def _():
|
117 |
-
import marimo as mo
|
118 |
-
return (mo,)
|
119 |
-
|
120 |
-
|
121 |
if __name__ == "__main__":
|
122 |
app.run()
|
|
|
7 |
|
8 |
import marimo
|
9 |
|
10 |
+
__generated_with = "0.10.13"
|
11 |
app = marimo.App()
|
12 |
|
13 |
|
14 |
+
@app.cell
|
15 |
+
def _():
|
16 |
+
import marimo as mo
|
17 |
+
return (mo,)
|
18 |
+
|
19 |
+
|
20 |
@app.cell(hide_code=True)
|
21 |
def _(mo):
|
22 |
mo.md(
|
23 |
"""
|
24 |
+
# 🎭 Strings
|
25 |
+
|
26 |
+
Dive into the world of Python strings — where text comes to life!
|
27 |
|
28 |
+
## Creating strings
|
29 |
+
In Python, strings are containers for text. You can create them in two simple
|
30 |
+
ways:
|
31 |
|
|
|
|
|
32 |
```python
|
33 |
greeting = "Hello, Python!" # using double quotes
|
34 |
+
name = 'Alice' # using single quotes
|
35 |
```
|
36 |
|
37 |
+
Below is an example string.
|
|
|
|
|
|
|
38 |
|
39 |
+
"""
|
40 |
+
)
|
41 |
+
return
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
@app.cell
|
45 |
+
def _():
|
46 |
+
text = "Python is amazing"
|
47 |
+
return (text,)
|
48 |
+
|
49 |
+
|
50 |
+
@app.cell(hide_code=True)
|
51 |
+
def _(mo):
|
52 |
+
mo.md(
|
53 |
+
"""
|
54 |
+
## Essential string operations
|
55 |
+
|
56 |
+
Here are some methods for working with strings.
|
57 |
+
|
58 |
+
Tip: Try changing the value of `text` above, and watch how the
|
59 |
+
computed values below change.
|
60 |
"""
|
61 |
)
|
62 |
return
|
63 |
|
64 |
|
65 |
@app.cell
|
66 |
+
def _(text):
|
67 |
+
len(text)
|
68 |
+
return
|
69 |
+
|
70 |
+
|
71 |
+
@app.cell
|
72 |
+
def _(text):
|
73 |
+
text.upper()
|
74 |
+
return
|
75 |
+
|
76 |
+
|
77 |
+
@app.cell
|
78 |
+
def _(text):
|
79 |
+
text.lower()
|
80 |
+
return
|
81 |
+
|
82 |
+
|
83 |
+
@app.cell
|
84 |
+
def _(text):
|
85 |
+
text.title()
|
86 |
+
return
|
87 |
+
|
88 |
+
|
89 |
+
@app.cell(hide_code=True)
|
90 |
+
def _(mo):
|
91 |
+
mo.md("Use string methods and the `in` operator to find things in strings.")
|
92 |
+
return
|
93 |
+
|
94 |
+
|
95 |
+
@app.cell
|
96 |
+
def _(text):
|
97 |
+
text.find('is')
|
98 |
+
return
|
99 |
+
|
100 |
+
|
101 |
+
@app.cell
|
102 |
+
def _(text):
|
103 |
+
"Python" in text
|
104 |
+
return
|
105 |
+
|
106 |
+
|
107 |
+
@app.cell
|
108 |
+
def _(text):
|
109 |
+
"Javascript" in text
|
110 |
+
return
|
111 |
+
|
112 |
+
|
113 |
+
@app.cell
|
114 |
+
def _(mo):
|
115 |
+
mo.md(
|
116 |
+
"""
|
117 |
+
## Inserting values in strings
|
118 |
+
|
119 |
+
Modern Python uses f-strings to insert values into strings. For example,
|
120 |
+
check out how the next cell greets you (and notice the `f''''`)!
|
121 |
+
|
122 |
+
Try changing the value of `my_name`, and watch how the greeting changes.
|
123 |
+
""")
|
124 |
+
return
|
125 |
+
|
126 |
+
|
127 |
+
@app.cell
|
128 |
+
def _():
|
129 |
+
my_name = ''
|
130 |
+
return (my_name,)
|
131 |
+
|
132 |
+
|
133 |
+
@app.cell
|
134 |
+
def _(my_name):
|
135 |
+
f"Hello, {my_name}!"
|
136 |
+
return
|
137 |
+
|
138 |
+
|
139 |
+
@app.cell(hide_code=True)
|
140 |
def _(mo):
|
141 |
mo.md(
|
142 |
"""
|
143 |
## Working with Parts of Strings
|
144 |
You can access any part of a string using its position (index):
|
145 |
+
"""
|
146 |
+
)
|
147 |
+
return
|
148 |
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
@app.cell
|
151 |
+
def _(text):
|
152 |
+
first_letter = text[0]
|
153 |
+
first_letter
|
154 |
+
return (first_letter,)
|
155 |
+
|
156 |
+
|
157 |
+
@app.cell
|
158 |
+
def _(text):
|
159 |
+
last_letter = text[-1]
|
160 |
+
last_letter
|
161 |
+
return (last_letter,)
|
162 |
+
|
163 |
+
|
164 |
+
@app.cell
|
165 |
+
def _(text):
|
166 |
+
first_three = text[0:3]
|
167 |
+
first_three
|
168 |
+
return (first_three,)
|
169 |
+
|
170 |
+
|
171 |
+
@app.cell
|
172 |
+
def _(text):
|
173 |
+
last_two = text[-2:]
|
174 |
+
last_two
|
175 |
+
return (last_two,)
|
176 |
+
|
177 |
+
|
178 |
+
@app.cell(hide_code=True)
|
179 |
+
def _(mo):
|
180 |
+
mo.md(
|
181 |
+
"""
|
182 |
+
## Other helpful string methods
|
183 |
+
|
184 |
+
Finally, here are some other helpful string methods. Feel free to try them out on your own strings!
|
185 |
+
|
186 |
```python
|
187 |
sentence = " python is fun "
|
188 |
|
|
|
205 |
return
|
206 |
|
207 |
|
208 |
+
@app.cell(hide_code=True)
|
209 |
def _(mo):
|
210 |
callout_text = mo.md("""
|
211 |
## Your String Journey Begins!
|
|
|
225 |
return (callout_text,)
|
226 |
|
227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
if __name__ == "__main__":
|
229 |
app.run()
|