Files
learn-python3/notebooks/beginner/exercises/01_strings_exercise.ipynb
Jerry Pussinen 52ca70b866 Pyupgrade 3.10+
2023-04-21 13:45:36 +03:00

157 lines
3.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Fill missing pieces\n",
"Fill `____` pieces below to have correct values for `lower_cased`, `stripped` and `stripped_lower_case` variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"original = \" Python strings are COOL! \"\n",
"lower_cased = original._____\n",
"stripped = ____.strip()\n",
"stripped_lower_cased = original._____._____"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's verify that the implementation is correct by running the cell below. `assert` will raise `AssertionError` if the statement is not true. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert lower_cased == \" python strings are cool! \"\n",
"assert stripped == \"Python strings are COOL!\"\n",
"assert stripped_lower_cased == \"python strings are cool!\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Prettify ugly string\n",
"Use `str` methods to convert `ugly` to wanted `pretty`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"ugly = \" tiTle of MY new Book\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation:\n",
"pretty = \"TODO\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make sure that it does what we want. `assert` raises [`AssertionError`](https://docs.python.org/3/library/exceptions.html#AssertionError) if the statement is not `True`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"print(f\"pretty: {pretty}\")\n",
"assert pretty == \"Title Of My New Book\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Format string based on existing variables\n",
"Create `sentence` by using `verb`, `language`, and `punctuation` and any other strings you may need."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"verb = \"is\"\n",
"language = \"Python\"\n",
"punctuation = \"!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation:\n",
"sentence = \"TODO\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"print(f\"sentence: {sentence}\")\n",
"assert sentence == \"Learning Python is fun!\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}