This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
informatique:python [2024/12/10 20:47] – yahiko | informatique:python [2024/12/13 21:11] (current) – yahiko | ||
---|---|---|---|
Line 85: | Line 85: | ||
===== Trouver la longueur d'une chaîne de caractères ===== | ===== Trouver la longueur d'une chaîne de caractères ===== | ||
<code python> | <code python> | ||
- | string = 'C'est vachement long quand même cette affaire dis donc.' | + | string = "C'est vachement long quand même cette affaire dis donc." |
print(len(string)) | print(len(string)) | ||
</ | </ | ||
+ | Va retourner '' | ||
+ | |||
+ | ===== Le dernier caractère d'une chaîne ===== | ||
+ | <code python> | ||
+ | print(" | ||
+ | </ | ||
+ | Va retourner '' | ||
===== Fonctions ===== | ===== Fonctions ===== | ||
Line 114: | 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 | ||
+ | </ | ||
+ | |||