{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[home](../index.ipynb) | [next: Un point sur les méthodes](methods.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Utilisation avancée des chaines de caractères\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Formatted string literals\n", "\n", "Plus souvent appelés [f-string](https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals), ils sont un moyen simple et efficace de produire des chaines formatées.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'*~ 2 ~*'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2\n", "f\"*~ {a} ~*\" # f-string !" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'*~ toto ~*'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'toto'\n", "f\"*~ {a} ~*\" # le même f-string mais la variable a une autre valeur" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Champs multiples" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'~1~ -2-'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "b = 2\n", "f\"~{a}~ -{b}-\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'~toto~ -tata-'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'toto'\n", "b = 'tata'\n", "f\"~{a}~ -{b}-\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'~42~ -tata-'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "b = 'tata'\n", "f\"~{a}~ -{b}-\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mise en forme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Des instructions de formatage peuvent être ajoutées `{variable:formatage}`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'| toto | tata | titi |'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# On peut justifier et centrer un champ\n", "a = 'toto'\n", "b = 'tata'\n", "c = 'titi'\n", "f\"| {a:>10} | {b:^10} | {c:<10} |\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", " 5\n", "005\n" ] } ], "source": [ "# On peut controler l'affichage des nombres\n", "a = 5\n", "print(f\"{a}\")\n", "print(f\"{a:3}\")\n", "print(f\"{a:03}\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.14159\n", "5\n", "5.000000\n", "5.000\n", "3.14\n", "5.000000e+00\n", "5.00e+00\n" ] } ], "source": [ "# et des nombres flottants\n", "a = 5\n", "pi = 3.14159\n", "print(f\"{pi}\") # sans formatage, affichage au plus près de la donnée\n", "print(f\"{a}\") # sans formatage, affichage au plus près de la donnée\n", "print(f\"{a:f}\") # affichons un int comme un float\n", "print(f\"{a:.3f}\") # controlons le nombre de décimales\n", "print(f\"{pi:.2f}\") # controlons le nombre de décimales\n", "print(f\"{a:e}\") # notation scientifique\n", "print(f\"{a:.2e}\") # notation scientifique avec 2 décimales" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Méthode des `str`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "str.capitalize(...)\n", "str.casefold(...)\n", "str.center(...)\n", "str.count(...)\n", "str.encode(...)\n", "str.endswith(...)\n", "str.expandtabs(...)\n", "str.find(...)\n", "str.format(...)\n", "str.format_map(...)\n", "str.index(...)\n", "str.isalnum(...)\n", "str.isalpha(...)\n", "str.isascii(...)\n", "str.isdecimal(...)\n", "str.isdigit(...)\n", "str.isidentifier(...)\n", "str.islower(...)\n", "str.isnumeric(...)\n", "str.isprintable(...)\n", "str.isspace(...)\n", "str.istitle(...)\n", "str.isupper(...)\n", "str.join(...)\n", "str.ljust(...)\n", "str.lower(...)\n", "str.lstrip(...)\n", "str.maketrans(...)\n", "str.partition(...)\n", "str.removeprefix(...)\n", "str.removesuffix(...)\n", "str.replace(...)\n", "str.rfind(...)\n", "str.rindex(...)\n", "str.rjust(...)\n", "str.rpartition(...)\n", "str.rsplit(...)\n", "str.rstrip(...)\n", "str.split(...)\n", "str.splitlines(...)\n", "str.startswith(...)\n", "str.strip(...)\n", "str.swapcase(...)\n", "str.title(...)\n", "str.translate(...)\n", "str.upper(...)\n", "str.zfill(...)\n" ] } ], "source": [ "# en python, les chaines de caractères disposent de nombreuses méthodes\n", "\n", "str_methods = [name for name in dir(str) if name[0] != '_']\n", "for name in str_methods:\n", " print(f'str.{name}(...)')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "des_mots = ['Bonjour', 'tout', 'le', 'monde', '!']\n", "mot = 'bidule'\n", "phrase = \"Vive les tartes a la myrtille.\"\n", "paragraphe = '''\n", "B: What is your name?\n", "A: It is Arthur, King of the Britons.\n", "B: What is your quest?\n", "A: To seek the Holy Grail.\n", "B: What is the air-speed velocity of a swallow?\n", "A: What do you mean? \n", "B: Er ... I don't know that ... Aaaaarrrrrrggghhh!\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tester le contenu" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n" ] } ], "source": [ "# tester la présence d'une sous chaine\n", "print('le' in des_mots) # élement dans une liste\n", "print('le' in mot)\n", "print('le' in phrase) # sous chaine" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "False\n" ] } ], "source": [ "# tester le début\n", "print(mot.startswith('b'))\n", "print(mot.startswith('bi'))\n", "print(mot.startswith('ba'))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "# tester la fin\n", "print(phrase.endswith('.'))\n", "print(phrase.endswith('myrtille'))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['isalnum',\n", " 'isalpha',\n", " 'isascii',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'isidentifier',\n", " 'islower',\n", " 'isnumeric',\n", " 'isprintable',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# tester la nature du contenu\n", "[name for name in dir(str) if name.startswith('is')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compter / localiser" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "3\n" ] } ], "source": [ "# str.count()\n", "\n", "print(paragraphe.count('?'))\n", "print(paragraphe.count('the'))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "18\n", "19\n" ] } ], "source": [ "# str.index()\n", "\n", "print(phrase.index('a'))\n", "print(phrase.index('la'))\n", "print(phrase.rindex('a'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fusionner, séparer" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"'Bonjour tout le monde !'\"" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.join()\n", "\n", "fatiguant = ''\n", "for elem in des_mots:\n", " if fatiguant:\n", " fatiguant += ' ' + elem\n", " else:\n", " fatiguant += elem\n", "repr(fatiguant)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"'Bonjour tout le monde !'\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "facile = ' '.join(des_mots)\n", "repr(facile)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Vive', 'les', 'tartes', 'a', 'la', 'myrtille.']" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.split()\n", "phrase.split()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Vive les ', 'ar', 'es a la myr', 'ille.']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.split()\n", "phrase.split('t')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['',\n", " 'B: What is your name?',\n", " 'A: It is Arthur, King of the Britons.',\n", " 'B: What is your quest?',\n", " 'A: To seek the Holy Grail.',\n", " 'B: What is the air-speed velocity of a swallow?',\n", " 'A: What do you mean? ',\n", " \"B: Er ... I don't know that ... Aaaaarrrrrrggghhh!\"]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.splitlines()\n", "paragraphe.splitlines()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Vive les t', 'a', 'rtes a la myrtille.')" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.partition()\n", "phrase.partition('a')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Vive les ', 'tartes', ' a la myrtille.')" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.partition()\n", "phrase.partition('tartes')" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Vive les tartes a l', 'a', ' myrtille.')" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# str.rpartition() pour partir de la droite\n", "phrase.rpartition('a')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mise en forme" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "vive les tartes a la myrtille.\n", "VIVE LES TARTES A LA MYRTILLE.\n", "Vive Les Tartes A La Myrtille.\n", "Vive les tartes a la myrtille.\n", "bidule Bidule\n" ] } ], "source": [ "# Changer la casse\n", "\n", "print(phrase.lower())\n", "print(phrase.upper())\n", "print(phrase.title())\n", "print(phrase.capitalize())\n", "print(mot, mot.capitalize())" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "' bidule'\n", "'bidule '\n", "' bidule '\n" ] } ], "source": [ "# justifier, centrer\n", "\n", "print(repr(mot.rjust(10)))\n", "print(repr(mot.ljust(10)))\n", "print(repr(mot.center(10)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Conversion de secondes (étape 3)](../exercises/exercises.ipynb#Exercice-3-:-conversion-de-secondes)\n", "\n", "``` python\n", "exercice = '''\n", " Définir un texte comme celui-ci et le découper en une liste de mots.\n", " Utiliser la fonction `compte_occurrence()` pour compter le nombre\n", " d'occurrences de chaque mot présent dans le texte.\n", "'''\n", "```" ] } ], "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }