Spaces:
Running
Running
File size: 3,858 Bytes
6cf1710 739cdef 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 739cdef e0a5cdf 739cdef e0a5cdf 739cdef e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 e0a5cdf 6cf1710 |
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 |
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.12"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# 🎭 Strings
Dive into the world of Python strings — where text comes to life!
## Creating strings
In Python, strings are containers for text. You can create them in two simple
ways:
```python
greeting = "Hello, Python!" # using double quotes
name = 'Alice' # using single quotes
```
Below is an example string.
"""
)
return
@app.cell
def _():
text = "Python is amazing"
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):
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):
text.find('is')
return
@app.cell
def _(text):
"Python" in text
return
@app.cell
def _(text):
"Javascript" in text
return
@app.cell
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 changing the value of `my_name`, and watch how the greeting changes.
"""
)
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!
```python
sentence = " python is fun "
# Remove extra spaces
print(sentence.strip()) # "python is fun"
# Split into a list of words
print(sentence.split()) # ['python', 'is', 'fun']
# Replace words
print(sentence.replace('fun', 'awesome'))
# Check what kind of text you have
print("123".isdigit()) # True - only numbers?
print("abc".isalpha()) # True - only letters?
print("Python3".isalnum()) # True - letters or numbers?
```
"""
)
return
@app.cell(hide_code=True)
def _(mo):
callout_text = mo.md("""
## Your String Journey Begins!
Next Steps:
- Try combining different string methods
- Practice with f-strings
- Experiment with string slicing
You're doing great! 🐍✨
""")
mo.callout(callout_text, kind="success")
return (callout_text,)
if __name__ == "__main__":
app.run()
|