{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Les chaînes de caractères" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I) Définition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### a) Définition directe" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mon chaîne de caractères\n" ] } ], "source": [ "s=\"mon chaîne de caractères\"\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### b) Par une entrée" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rentrez une chaîne : texte\n", "texte\n" ] } ], "source": [ "s= input(\"rentrez une chaîne : \")\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### c) Comme une sous-chaîne\n", "On procède comme pour une liste." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cde\n" ] } ], "source": [ "s=\"abcdefghijklm\"\n", "part=s[2:5]\n", "print(part)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## II) Parcours d'une chaîne\n", "Là encore, tout fonctionne comme pour une liste." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B\n", "o\n", "n\n", "j\n", "o\n", "u\n", "r\n" ] } ], "source": [ "mot=\"Bonjour\"\n", "for lettre in mot :\n", " print(lettre)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## III) Opérations sur les chaînes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### a) Longueur d'une chaîne" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "mot=\"Bonjour\"\n", "print(len(mot))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### b) Opérations sur la casse" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "BONJOUR\n" ] } ], "source": [ "# tout en majuscules\n", "mot=\"Bonjour\"\n", "mot=mot.upper()\n", "print(mot)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bonjour\n" ] } ], "source": [ "#Tout en minuscules\n", "mot=\"Bonjour\"\n", "mot=mot.lower()\n", "print(mot)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bonjour\n" ] } ], "source": [ "# seule la première lettre en majuscule\n", "mot=\"bonJOUR\"\n", "mot=mot.title()\n", "print(mot)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### c) Concaténation" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Le petit\n" ] } ], "source": [ "#méthode efficace pour 2 ou 3 chaînes\n", "mot1=\"Le\"\n", "mot2=\"petit\"\n", "phrase=mot1+\" \"+mot2\n", "print(phrase)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Le petit chat de mon voisin\n", "Le hum petit hum chat hum de hum mon hum voisin\n" ] } ], "source": [ "#méthode efficace pour des listes de mots\n", "mots=[\"Le\",\"petit\",\"chat\",\"de\",\"mon\",\"voisin\"]\n", "phrase=\" \".join(mots)\n", "print(phrase)\n", "phrase=\" hum \".join(mots)\n", "print(phrase)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### d) Séparation d'une chaîne en liste" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Je', 'suis', 'vraiment', 'satisfait', 'par', 'mon', 'excellent', 'travail.']\n" ] } ], "source": [ "# par défaut, le séparateur est l'espace\n", "phrase=\"Je suis vraiment satisfait par mon excellent travail.\"\n", "L=phrase.split()\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Je', 'suis', 'vraiment', 'satisfait', 'par', 'mon', 'excellent', 'travail.']\n" ] } ], "source": [ "# on spécifie un séparateur en argument\n", "phrase=\"Je--suis--vraiment--satisfait--par--mon--excellent--travail.\"\n", "L=phrase.split(\"--\")\n", "print(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }