Haleshot commited on
Commit
5251293
·
unverified ·
1 Parent(s): 74d1ccb

Add string manipulation notebook

Browse files
Files changed (1) hide show
  1. Python/Phase1/string-manipulation.py +179 -0
Python/Phase1/string-manipulation.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.10"
3
+ # dependencies = [
4
+ # "marimo",
5
+ # ]
6
+ # ///
7
+
8
+ import marimo
9
+
10
+ __generated_with = "0.10.12"
11
+ app = marimo.App(width="columns")
12
+
13
+
14
+ @app.cell(column=0)
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
+ # 🐍 Python Strings: Your First Data Type
25
+
26
+ Welcome to your first Python lesson! Today, we'll explore strings - one of Python's fundamental data types.
27
+
28
+ ## What are Strings?
29
+ Strings are sequences of characters - like words, sentences, or any text. In Python, we create strings by
30
+ enclosing text in either single (`'`) or double (`"`) quotes.
31
+
32
+ ```python
33
+ greeting = "Hello, Python!"
34
+ name = 'Alice'
35
+ ```
36
+ """
37
+ )
38
+ return
39
+
40
+
41
+ @app.cell
42
+ def _(input_text):
43
+ input_text
44
+ return
45
+
46
+
47
+ @app.cell
48
+ def _(mo):
49
+ input_text = mo.ui.text(
50
+ value="Hello, World!",
51
+ placeholder="Type any text here...",
52
+ label="Create your first string"
53
+ )
54
+ return (input_text,)
55
+
56
+
57
+ @app.cell
58
+ def _(input_text, mo):
59
+ mo.md(f"""
60
+ ### Your String Analysis
61
+
62
+ Let's analyze the string you created:
63
+
64
+ - Your string: `"{input_text.value}"`
65
+
66
+ - Length: `{len(input_text.value)}`
67
+
68
+ - First character: `'{input_text.value[0] if input_text.value else ''}'`
69
+
70
+ - Last character: `'{input_text.value[-1] if input_text.value else ''}'`
71
+ """)
72
+ return
73
+
74
+
75
+ @app.cell
76
+ def _(operation):
77
+ operation
78
+ return
79
+
80
+
81
+ @app.cell
82
+ def _(input_text, mo, operation, result):
83
+ mo.md(f"""
84
+ ### String Operation Result
85
+
86
+ Original: `{input_text.value}`
87
+
88
+ Result: `{result}`
89
+
90
+ Python code representation:
91
+ ```python
92
+ text = "{input_text.value}"
93
+ result = text.{operation.selected_key}()
94
+ print(result) # {result}
95
+ ```
96
+ """)
97
+ return
98
+
99
+
100
+ @app.cell
101
+ def _(mo):
102
+
103
+ operation = mo.ui.dropdown(
104
+ options={
105
+ "upper": "Convert to UPPERCASE",
106
+ "lower": "Convert to lowercase",
107
+ "title": "Convert To Title Case",
108
+ "strip": "Remove extra spaces"
109
+ },
110
+ value="upper",
111
+ label="Choose a string operation"
112
+ )
113
+ return (operation,)
114
+
115
+
116
+ @app.cell
117
+ def _(input_text, operation):
118
+ operations = {
119
+ "Convert to UPPERCASE": input_text.value.upper(),
120
+ "Convert to lowercase": input_text.value.lower(),
121
+ "Convert To Title Case": input_text.value.title(),
122
+ "Remove extra spaces": input_text.value.strip()
123
+ }
124
+
125
+ result = operations[operation.value]
126
+ return operations, result
127
+
128
+
129
+ @app.cell(hide_code=True)
130
+ def _(mo):
131
+ slice_text = mo.ui.text(
132
+ value="Python",
133
+ placeholder="Enter text to slice",
134
+ label="Text for slicing"
135
+ )
136
+
137
+ start_idx = mo.ui.number(
138
+ value=0,
139
+ start=0,
140
+ stop=10,
141
+ label="Start Index"
142
+ )
143
+
144
+ end_idx = mo.ui.number(
145
+ value=3,
146
+ start=0,
147
+ stop=10,
148
+ label="End Index"
149
+ )
150
+ return end_idx, slice_text, start_idx
151
+
152
+
153
+ @app.cell(column=1)
154
+ def _(end_idx, slice_text, start_idx):
155
+ slice_text, start_idx, end_idx
156
+ return
157
+
158
+
159
+ @app.cell
160
+ def _(end_idx, mo, slice_text, start_idx):
161
+ sliced = slice_text.value[start_idx.value:end_idx.value]
162
+ mo.md(f"""
163
+ ### String Slicing
164
+
165
+ Text: `{slice_text.value}`
166
+
167
+ Slice `[{start_idx.value}:{end_idx.value}]`: `{sliced}`
168
+
169
+ ```python
170
+ text = "{slice_text.value}"
171
+ slice = text[{start_idx.value}:{end_idx.value}]
172
+ print(slice) # {sliced}
173
+ ```
174
+ """)
175
+ return (sliced,)
176
+
177
+
178
+ if __name__ == "__main__":
179
+ app.run()