Spaces:
Running
Running
| # /// script | |
| # requires-python = ">=3.10" | |
| # dependencies = [ | |
| # "marimo", | |
| # ] | |
| # /// | |
| import marimo | |
| __generated_with = "0.10.19" | |
| app = marimo.App(width="medium") | |
| 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 | |
| def _(): | |
| text = "Python is amazing!" | |
| text | |
| return (text,) | |
| 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 | |
| def _(text): | |
| # the `len` method returns the number of characters in the string. | |
| len(text) | |
| return | |
| def _(text): | |
| text.upper() | |
| return | |
| def _(text): | |
| text.lower() | |
| return | |
| def _(text): | |
| text.title() | |
| return | |
| def _(mo): | |
| mo.md("""Use string methods and the `in` operator to find things in strings.""") | |
| return | |
| def _(text): | |
| # Returns the index of "is" in the string | |
| text.find("is") | |
| return | |
| def _(text): | |
| "Python" in text | |
| return | |
| def _(text): | |
| "Javascript" in text | |
| return | |
| 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 | |
| def _(): | |
| my_name = "" | |
| return (my_name,) | |
| def _(my_name): | |
| f"Hello, {my_name}!" | |
| return | |
| def _(mo): | |
| mo.md( | |
| """ | |
| ## Working with parts of strings | |
| You can access any part of a string using its position (index): | |
| """ | |
| ) | |
| return | |
| def _(text): | |
| first_letter = text[0] | |
| first_letter | |
| return (first_letter,) | |
| def _(text): | |
| last_letter = text[-1] | |
| last_letter | |
| return (last_letter,) | |
| def _(text): | |
| first_three = text[0:3] | |
| first_three | |
| return (first_three,) | |
| def _(text): | |
| last_two = text[-2:] | |
| last_two | |
| return (last_two,) | |
| 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 | |
| def _(): | |
| sentence = " python is fun " | |
| sentence | |
| return (sentence,) | |
| def _(sentence): | |
| # Remove extra spaces | |
| sentence.strip() | |
| return | |
| def _(sentence): | |
| # Split into a list of words | |
| sentence.split() | |
| return | |
| def _(sentence): | |
| sentence.replace("fun", "awesome") | |
| return | |
| def _(): | |
| "123".isdigit(), "abc".isdigit() | |
| return | |
| def _(): | |
| "123".isalpha(), "abc".isalpha() | |
| return | |
| def _(): | |
| "Python3".isalnum() | |
| return | |
| 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 | |
| def _(): | |
| import marimo as mo | |
| return (mo,) | |
| if __name__ == "__main__": | |
| app.run() | |