280 lines
5.8 KiB
Plaintext
280 lines
5.8 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "df41324e-da47-4d61-a80d-a7bd29198c8d",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Texte\n",
|
|||
|
"\n",
|
|||
|
"Bisher haben wir nur mit Zahlen gearbeitet, d.h. Python als besseren Taschenrechner verwendet.\n",
|
|||
|
"Jetzt wollen wir auch Texte verwenden.\n",
|
|||
|
"\n",
|
|||
|
"Text wird auch Zeichenkette oder String genannt.\n",
|
|||
|
"\n",
|
|||
|
"Texte haben wir auch schon gesehen, z.B.:\n",
|
|||
|
"\n",
|
|||
|
"* Variablennamen `a`, `b`, `c`\n",
|
|||
|
"* Funktionsnamen wie `my_min` und `cos`\n",
|
|||
|
"\n",
|
|||
|
"Um Texte, mit denen wir rechnen wollen, von Variablen und Funktionen zu unterscheiden, müssen wir diese in \" oder ' einschließen."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"id": "7ae417ae-c8d5-4f0e-a280-16265f3fa2d3",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"a = 'Wort'\n",
|
|||
|
"b = \"Zwei Worte\"\n",
|
|||
|
"c = \"\"\"Ein mehrzeiliger Text\n",
|
|||
|
"muss in\n",
|
|||
|
"drei Hochkommatas\n",
|
|||
|
"eingeschlossen werden.\"\"\""
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"id": "8af71ec8-1c40-4106-bbd4-3b0bdd2968f7",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'Wort'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"a"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"id": "14c7d057-124d-4e3b-b6be-a966687930e1",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'Ein mehrzeiliger Text\\nmuss in\\ndrei Hochkommatas\\neingeschlossen werden.'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"c"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "2e280d2d-eda2-473d-8997-94de82444f48",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"ℹ️ mit \\ lassen sich Sonderzeichen darstellen\n",
|
|||
|
"\n",
|
|||
|
"* \\\\ = \\\n",
|
|||
|
"* \\n = Zeilenumbruch\n",
|
|||
|
"* \\t = Tabulator"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "41dc4823-c5f0-4ffb-a6c6-c091789cf309",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Aufgabe\n",
|
|||
|
"❓ Was kann man alles mit Texten machen?\n",
|
|||
|
"\n",
|
|||
|
"Tipp:\n",
|
|||
|
"* `c.<TAB>`\n",
|
|||
|
"* `help(str)`"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "a376173a-66db-485f-acfe-818c44ad7320",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Lösung\n",
|
|||
|
"Die Aufgabe war so einfach, die braucht keine Lösung."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "d6a2bb1b-120c-48bf-aed5-ec11fe83f50a",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Aufgabe\n",
|
|||
|
"❓ Schreibt eine Funktion, die als Parameter ein englisches Wort akzeptiert und dessen Pluralform zurückliefert."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "59669fc1-2dc6-43a6-a297-3304f190d8ea",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Lösung\n",
|
|||
|
"Wir ignorieren die Sonderfälle und beschränken uns darauf, dBuchstaben **s** anzufügen wenn das Wort nicht damit endet."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"id": "45aa62f3-1257-4499-b2fb-b818cc1dbd21",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": [
|
|||
|
"def plural(s):\n",
|
|||
|
" if s.endswith('s'):\n",
|
|||
|
" return s\n",
|
|||
|
" else:\n",
|
|||
|
" return s + 's'"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"id": "ddfbc878-4c16-48a8-852c-97379f447ab9",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'horses'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"plural(\"horse\")"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "0e1833b4-aa3b-41b4-9602-0e36103e0070",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Rechnen mit Texten\n",
|
|||
|
"Texte kann man addieren und multiplizieren:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 7,
|
|||
|
"id": "17a904df-8ffc-467a-8744-e2a4fd8b2134",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'Hallo Python'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 7,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"a = \"Hallo\"\n",
|
|||
|
"b = \"Python\"\n",
|
|||
|
"a + ' ' + b"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 8,
|
|||
|
"id": "8c7599a2-c199-433b-bc74-5da51688bdd1",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'HalloHalloHallo'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 8,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"a * 3"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"id": "854d1c91-d4ea-478f-822e-3a3736fb5ff3",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Das kann man z.B. nutzen, um einen langen Strich zu erzeugen:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 11,
|
|||
|
"id": "ef7ad859-36f3-46a4-9f5c-972eb45ad0e7",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"'________________________________________________________________________________'"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 11,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"'_' * 80"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": null,
|
|||
|
"id": "cf941b04-99bd-41ce-ac04-4fd3b7a9564b",
|
|||
|
"metadata": {},
|
|||
|
"outputs": [],
|
|||
|
"source": []
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 3 (ipykernel)",
|
|||
|
"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.9.7"
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 5
|
|||
|
}
|