This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatique:python [2024/11/25 09:02] – yahiko | informatique:python [2024/12/13 21:11] (current) – yahiko | ||
---|---|---|---|
Line 82: | Line 82: | ||
print(" | print(" | ||
</ | </ | ||
+ | |||
+ | ===== Trouver la longueur d'une chaîne de caractères ===== | ||
+ | <code python> | ||
+ | string = " | ||
+ | print(len(string)) | ||
+ | </ | ||
+ | Va retourner '' | ||
+ | |||
+ | ===== Le dernier caractère d'une chaîne ===== | ||
+ | <code python> | ||
+ | print(" | ||
+ | </ | ||
+ | Va retourner '' | ||
===== Fonctions ===== | ===== Fonctions ===== | ||
Line 108: | Line 121: | ||
print mot_sansaccents | print mot_sansaccents | ||
</ | </ | ||
+ | |||
+ | === Raccourcir une chaîne === | ||
+ | <code python> | ||
+ | def shorten(short): | ||
+ | # On récupère le nombre de caractères de la chaîne en integer | ||
+ | nb = int(len(short)) | ||
+ | # Si supérieur à la variable qui définir la longueur on raccourci | ||
+ | if nb > lenght: | ||
+ | string = str(short[0: | ||
+ | # Sinon on affiche tout | ||
+ | else: | ||
+ | string = short | ||
+ | return string | ||
+ | # Longueur max et longueur retournée | ||
+ | lenght = 20 | ||
+ | chaine = " | ||
+ | |||
+ | print(shorten(chaine)) | ||
+ | </ | ||
+ | |||
+ | === Faire un truc si le dernier caractère d'une chaîne est quelque chose === | ||
+ | Je ne sais pas, un '' | ||
+ | <code python> | ||
+ | def DashCheck(check): | ||
+ | if check == ' | ||
+ | dash = str(" | ||
+ | else: | ||
+ | dash = str(" | ||
+ | return dash | ||
+ | |||
+ | print(DashCheck(" | ||
+ | </ | ||
+ | Va retourner '' | ||
+ | |||
+ | Et si on veut supprimer ce dernier '' | ||
+ | <code python> | ||
+ | def DashCheck(check): | ||
+ | # On vérifie le dernier caractère de la chaîne | ||
+ | if check[-1] == ' | ||
+ | # Si " | ||
+ | dash = check[:-1] | ||
+ | return dash | ||
+ | else: | ||
+ | return check | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||