Spaces:
Sleeping
Sleeping
Update string notebook
Browse files
Python/Phase1/string-manipulation.py
DELETED
@@ -1,179 +0,0 @@
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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"
|
3 |
+
# dependencies = [
|
4 |
+
# "marimo",
|
5 |
+
# ]
|
6 |
+
# ///
|
7 |
+
|
8 |
+
import marimo
|
9 |
+
|
10 |
+
__generated_with = "0.10.12"
|
11 |
+
app = marimo.App()
|
12 |
+
|
13 |
+
|
14 |
+
@app.cell(hide_code=True)
|
15 |
+
def _(mo):
|
16 |
+
mo.md(
|
17 |
+
"""
|
18 |
+
# 🎭 Python Strings
|
19 |
+
|
20 |
+
Dive into the fascinating world of Python strings - where text becomes magic!
|
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' # using single quotes
|
27 |
+
```
|
28 |
+
|
29 |
+
## Essential String Operations
|
30 |
+
Let us explore what you can do with strings:
|
31 |
+
```python
|
32 |
+
text = "Python is amazing!"
|
33 |
+
|
34 |
+
# Basic operations
|
35 |
+
print(len(text)) # Count characters: 17
|
36 |
+
print(text.upper()) # PYTHON IS AMAZING!
|
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 |
+
## String Formatting Made Easy
|
46 |
+
Modern Python uses f-strings - they are the easiest way to add variables to your text:
|
47 |
+
```python
|
48 |
+
name = "Alice"
|
49 |
+
age = 25
|
50 |
+
message = f"Hi, I'm {name} and I'm {age} years old!"
|
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 |
+
```python
|
64 |
+
text = "Python"
|
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 |
+
## Common String Methods You'll Love
|
73 |
+
```python
|
74 |
+
sentence = " python is fun "
|
75 |
+
|
76 |
+
# Remove extra spaces
|
77 |
+
print(sentence.strip()) # "python is fun"
|
78 |
+
|
79 |
+
# Split into a list of words
|
80 |
+
print(sentence.split()) # ['python', 'is', 'fun']
|
81 |
+
|
82 |
+
# Replace words
|
83 |
+
print(sentence.replace('fun', 'awesome'))
|
84 |
+
|
85 |
+
# Check what kind of text you have
|
86 |
+
print("123".isdigit()) # True - only numbers?
|
87 |
+
print("abc".isalpha()) # True - only letters?
|
88 |
+
print("Python3".isalnum()) # True - letters or numbers?
|
89 |
+
```
|
90 |
+
"""
|
91 |
+
)
|
92 |
+
return
|
93 |
+
|
94 |
+
|
95 |
+
@app.cell
|
96 |
+
def _(mo):
|
97 |
+
callout_text = mo.md("""
|
98 |
+
## Your String Journey Begins!
|
99 |
+
|
100 |
+
Next Steps:
|
101 |
+
|
102 |
+
- Try combining different string methods
|
103 |
+
|
104 |
+
- Practice with f-strings
|
105 |
+
|
106 |
+
- Experiment with string slicing
|
107 |
+
|
108 |
+
You're doing great! 🐍✨
|
109 |
+
""")
|
110 |
+
|
111 |
+
mo.callout(callout_text, kind="success")
|
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()
|