# Strings ```python >>> mon_str = "bonjour" >>> print(mon_str) bonjour >>> long_str = """*********** ... * Bonjour * ... *********** ... """ >>> long_str '***********\n* Bonjour *\n***********\n' >>> print(long_str) *********** * Bonjour * *********** ``` ```python # Concaténation >>> mon_str + " Bruno" 'bonjour Bruno' # Répétition >>> "ha " * 5 'ha ha ha ha ha ' # Longueur de la chaine >>> len(mon_str) 7 # Indice >>> mon_str[0] 'b' >>> mon_str[6] 'r' >>> mon_str[len(mon_str)-1] 'r' >>> mon_str[-1] 'r' >>> mon_str[-3] 'o' ``` ##### str.count() ```python # -compte le nombre d'occurence de la chaine demandée >>> "programmation".count("m") 2 ``` ##### str.capitalize() ```python # -capitalise la chaine de caractère >>> "pYtHOn".capitalize() 'Python' ``` ##### str.endswith(str) ```python # -teste si une chaine se termine par la chaine demandée (sensible à la casse) >>> a = "Python" >>> a.endswith("r") False >>> a.endswith("n") True ``` ##### str.find() ```python # -trouve la 1ere occurence de la chaine demandée >>> "capitalize".find("i") 3 ``` ##### str.isalnum() ```python # -retourne True si tous les caractères sont alphanumériques >>> "0xf".isalnum() True >>> "0xf_".isalnum() False ``` ##### str.isalpha() ```python # -retourne True si tous les caractères sont des lettres >>> "0xf".isalpha() False >>> "abcdef".isalpha() True ``` ##### str.isdigit() ```python # -retourne True si tous les caractères sont numériques >>> "1234".isalnum() True ``` ##### str.islower() ```python # -retourne True si tous les caractères sont en minuscule >>> "python".islower() True ``` ##### str.isspace() ```python # -retourne True si tous les caractères sont des espaces >>> "".isspace() False >>> " ".isspace() True >>> "un mot".isspace() False ``` ##### str.istitle() ```python # -retourne True si la chaine à un format titre >>> "Python".istitle() True >>> "Python Est Un Language De Programmation".istitle() True ``` ##### str.isupper() ```python # -retourne True si tous les caractères sont en majuscule >>> "PYTHON".isupper() True ``` ##### str.join(liste) ```python # -transforme une liste en chaine >>> ":".join(["language", "python"]) 'language:python' ``` ##### len(s) ```python # -retourne le nb d'items d'un objet >>> len(["language", "python"]) 2 >>> len("Python") 6 ``` ##### str.lower() ```python # -retourne la chaine en minuscule >>> "PYTHON".lower() 'python' ``` ##### str.replace(str,str) ```python # -remplace un segement de chaine par un autre >>> "Python est un language".replace("Python","Perl") 'Perl est un language' ``` ##### str.startswith() ```python # -retourne True si la chaine commence par le préfix indiqué (sensible à la casse) >>> a = "Python" >>> a.startswith("P") True >>> a.startswith("y") False ``` ##### str.split(séparateur) ```python # -transforme une chaine en liste >>> "Language:Python".split(":") ['Language', 'Python'] ``` ##### str.splitlines() ```python # -retourne une liste des lignes de la chaine >>> "language\n\n\Python\n\n3.7".splitlines() ['language', '', '\\Python', '', '3.7'] >>> "language\nPython\n3.7".splitlines() ['language', 'Python', '3.7'] >>> "language\n\rPython\n\r3.7".splitlines() ['language', '', 'Python', '', '3.7'] >>> "language\r\nPython\r\n3.7".splitlines() ['language', 'Python', '3.7'] >>> "language\r\nPython\r\n\r\n3.7".splitlines() ['language', 'Python', '', '3.7'] >>> "language\r\nPython\r\n\r\n3.7".splitlines(True) ['language\r\n', 'Python\r\n', '\r\n', '3.7'] ``` ##### str.title() ```python # -transforme la chaine dans un format Titre >>> "Ceci est un titre".title() 'Ceci Est Un Titre' ``` #####