{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Fortgeschrittene Strings\n", "\n", "String Objekte bieten eine Vielzahl an Methoden, die uns Zeit sparen und Funktionalität erhöhen. In dieser Lektion werden wir einige davon ausprobieren:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = \"hallo Welt\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Groß-/Kleinschreibung\n", "\n", "Wir können Methoden nutzen, um den ersten Buchstaben groß zu schreiben oder alle Wörter eines Strings klein bzw. groß zu schreiben." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hallo welt'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Den ersten Buchstaben eines Strings groß schreiben\n", "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HALLO WELT'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo welt'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Platzierung und Zählung" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('o')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatierung\n", "\n", "Die center() Methode erlaubt es uns, unseren String in der Mitte innerhalb eines Strings mit einer bestimmten Länge zu platzieren. Aber um ehrlich zu sein, kam diese Methode in keiner meiner Codes zum Einsatz." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'zzzzzHallo Weltzzzzz'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.center(20,'z')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo hi'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# expandtabs() wandelt Tab Notation \\t in Leerzeichen\n", "'hallo\\thi'.expandtabs()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## is Check Methoden\n", "\n", "Diese folgenden Methoden überprüfen, ob der String bestimmte Bedingungen erfüllt. Schauen wir sie uns an:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = 'Hallo'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isalnum() gibt True (wahr) aus, wenn alle Zeichen im String alphanumerisch sind." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalnum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isalpha() gibt True zurück, falls alle Zeichen im String dem Alphabet zugehörig sind." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalpha()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "islower() gibt True aus, falls alle Zeichen im String klein geschrieben sind. Andernfalls erhalten wir False." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.islower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Um zu überprüfen ob alle Zeichen eines Strings Leerzeichen sind, verwendet man isspace()." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isspace()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "istitle() ergibt True, wenn ein String groß- und kleingeschrieben ist wie ein Titel." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.istitle()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "isupper() gibt True aus, falls alle Zeichen im String groß geschrieben sind. Andernfalls erhalten wir False." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isupper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eine weitere Methode ist endswith(), was im Endeffekt eine Boolean Überprüfung von s[-1] ist." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.endswith('o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vorinstallierte Regular Expression\n", "\n", "Strings haben einige vorinstallierte Methoden, die Regular Expression Operationen wiederspiegeln können. Wir könne split() benutzen, um den String bei einem bestimmten Element zu trennen und das Ergebnis in einer Liste auszugeben.\n", "\n", "Wir können außerdem partition() benutzen, um ein Tupel zu erzeugen, das den Teiler (erste Erscheinung) sowie die vordere und hintere Hälfte beinhaltet." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['H', 'llo']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.split('a')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Ha', 'l', 'lo')" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.partition('l')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hallo'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wunderbar! Ihr solltet euch jetzt wohl fühlen im Umgang der zahlreichen vorinstallierten String Methoden in Python!" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }