len()Le stringhe in Python sono sequenze di caratteri, e ogni carattere — inclusi spazi, virgole e simboli — possiede una posizione chiamata indice. Capire come funziona l’indicizzazione è fondamentale per leggere, analizzare o modificare il testo nei tuoi programmi.
Python assegna un indice a ogni carattere della stringa. L’indicizzazione parte da 0, quindi:
E così via.
Esempio con la stringa "Apple":
| A | p | p | l | e |
|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 |
Python permette anche di contare i caratteri partendo dalla fine.
La stessa stringa "Apple" vista da destra:
| A | p | p | l | e |
|---|---|---|---|---|
| -5 | -4 | -3 | -2 | -1 |
Questo è molto utile quando non conosci la lunghezza esatta della stringa.
grocery_item = "Milk"
# Accessing the first and last character using indexing
first_character = grocery_item[0] # 'M'
last_character = grocery_item[-1] # 'k'
print("First character:", first_character)
print("Last character:", last_character)
len() per misurare la lunghezza della stringaLa funzione len() restituisce il numero totale di caratteri, inclusi gli spazi.
store_name = "Green Valley Market"
length_of_name = len(store_name) # Include gli spazi
character_after_space = store_name[6] # 'V'
print("Length of store name:", length_of_name)
print("Character after the space:", character_after_space)
Dati tre prodotti nello stesso campo grocery_item, usa l’indicizzazione e len() per ricavare informazioni utili.
Puoi usare questa stringa come riferimento:
"Milk Bread Eggs"
length_of_itemfirst_char, second_char, third_charlast_char1, last_char2, last_char3grocery_item = "Milk Bread Eggs"
# Length of the string
length_of_item = len(grocery_item)
# Positive indexing (first character of each word)
first_char = grocery_item[0] # 'M'
second_char = grocery_item[5] # 'B'
third_char = grocery_item[11] # 'E'
# Negative indexing (last character of each word)
last_char1 = grocery_item[3] # 'k'
last_char2 = grocery_item[9] # 'd'
last_char3 = grocery_item[-1] # 's'
print("Length:", length_of_item)
print("First letters:", first_char, second_char, third_char)
print("Last letters:", last_char1, last_char2, last_char3)