#Définir une chaîne
A = 'Ichiro'
print(A)
#Concaténation de chaînes
B = "Suzuki"
C = A+B
print(C)
#Répétez la chaîne.
say = "This is important."
say_two = say*2
say_three = say*3
print(say)
print(say_two)
print(say_three)
print("-"*20)
#Indique la longueur.
print( len(say) )
print( len(say_two) )
print( len(say_three) )
print("-"*20)
#Rechercher des chaînes
D = "My name is Hideki Matsui."
print( "Is the word *is* included in D?")
print( "is" in D )
print( "Is the word *are* included in D?")
print( "are" in D )
print("-"*20)
#Examiner l'élément
print(D[0])
print(D[5])
print(D[10])
print(D[15])
print(D[5:15]) # slice
A = [1,2,3,4,5]
print(A[0])
print(A[2])
print(A[-1])
print(A[1:3]) # "slice"
print(A)
B = [100,101,102]
print( A + [100,101,102] )
print( A + [100,] )
print( A + [100] )
print(A)
#Je ne peux pas faire ça:
# print(A + 100)
#Vous pouvez également utiliser append.
A.append(100)
#Il y a aussi une extension.
A.extend([200,201,202])
print(A)
print('-'*20)
print(A)
print(A[2])
A[2] = 2020
print(A)
print(A[2])
print('-'*20)
del A[3]
print(A)
print(A[3])
print('-'*20)
print( 1 in A )
print( 2 in A )
print( 3 in A )
print( 4 in A )
print( 5 in A )
print('++'*20)
print( 100 in A )
print( 101 in A )
print( 102 in A )
print('=='*20)
print( 2020 in A )
print('..'*20)
Vous pouvez vérifier la position (adresse) de l'élément dans la liste avec la méthode d'index.
list_names = ['Tom', 'Jack', 'Bob', 'Andy']
print(list_names.index('Jack'))
print(list_names.index('Andy'))
print(list_names.index('ANDY'))
#Cela ne marche pas. Cela provoquera une erreur, utilisez donc une instruction if pour l'éviter.
if ( 'ANDY' in list_names):
print( list_names.index('ANDY') )
else :
print( 'ANDY is an unknown name.')
if ( 'Jack' in list_names):
print( list_names.index('Jack') )
else :
print( 'Jack is an unknown name.')
Recommended Posts