Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Indicizzazione delle Stringhe e Lunghezza | Variabili e Tipi
Introduzione a Python

book
Indicizzazione delle Stringhe e Lunghezza

Le stringhe in Python sono sequenze di caratteri in cui a ciascun carattere, inclusi gli spazi, viene assegnata una posizione o indice specifico. Apprendere come accedere a questi caratteri tramite l'indicizzazione e determinare la lunghezza delle stringhe utilizzando la funzione len() rappresenta una competenza fondamentale in Python.

Guarda il seguente video, in cui Alex dimostra come l'indicizzazione e la funzione len() possano essere utilizzate per interagire efficacemente con le stringhe.

In Python, l'indicizzazione inizia da 0, quindi il primo carattere di una stringa si trova all'indice 0, il secondo all'indice 1 e così via. Questo è spesso indicato come la regola n-1, dove n rappresenta il numero di caratteri nella stringa. Per visualizzare meglio questo concetto, considera la stringa "Apple":

Indicizzazione Negativa

Al contrario, l'indicizzazione negativa consente di contare i caratteri a partire dalla fine della stringa invece che dall'inizio.

Questo metodo è particolarmente utile quando si desidera accedere agli ultimi elementi di una stringa senza conoscerne la lunghezza esatta. L'ultimo carattere della stringa ha indice -1, il penultimo -2 e così via.

Esploriamo la stessa stringa, "Apple", utilizzando indici negativi per evidenziare come ogni carattere possa essere raggiunto partendo dalla fine:

Applicazione di esempio

Iniziamo con le basi dell'indicizzazione delle stringhe. Utilizza questo esempio per provare a stampare diversi caratteri dalla stringa. Puoi anche provare a utilizzare l'indicizzazione negativa.

grocery_item = "Milk"

# Accessing the first and last character using indexing
first_character = grocery_item[0] # 'M'
last_character = grocery_item[-1] # 'k', using negative indexing for the last character

print("First character:", first_character)
print("Last character:", last_character)
12345678
grocery_item = "Milk" # Accessing the first and last character using indexing first_character = grocery_item[0] # 'M' last_character = grocery_item[-1] # 'k', using negative indexing for the last character print("First character:", first_character) print("Last character:", last_character)
copy

Ora esploriamo una stringa con spazi e utilizziamo la funzione len() per vedere come gli spazi vengono conteggiati come caratteri.

Comprendere che gli spazi sono considerati caratteri in Python può aiutare a manipolare correttamente le stringhe, soprattutto quando fanno parte dei dati.

store_name = "Green Valley Market"

# Find the length of the string, which includes spaces
length_of_name = len(store_name) # Includes spaces in the count

# Accessing a character in a position after a space
character_after_space = store_name[6] # 'V'

print("Length of store name:", length_of_name)
print("Character after the space:", character_after_space)
12345678910
store_name = "Green Valley Market" # Find the length of the string, which includes spaces length_of_name = len(store_name) # Includes spaces in the count # Accessing a character in a position after a space character_after_space = store_name[6] # 'V' print("Length of store name:", length_of_name) print("Character after the space:", character_after_space)
copy
Compito

Swipe to start coding

Utilizzare l'indicizzazione delle stringhe per estrarre caratteri specifici da una stringa data. Calcolare la lunghezza della stringa utilizzando len().

  • Utilizzare len() per ottenere la lunghezza della stringa grocery_item e memorizzarla in length_of_item.
  • Utilizzare indicizzazione positiva per ottenere il primo carattere di ogni parola in grocery_item e assegnarli a first_char, second_char e third_char.
  • Utilizzare indicizzazione negativa per ottenere l'ultimo carattere di ogni parola e assegnarli a last_char1, last_char2 e last_char3.

Soluzione

grocery_item = "Grilled Chicken Salad"

# Utilize the len() function to find the length of the string
length_of_item = len(grocery_item)

# Use positive indexing to find the first character of each word
first_char = grocery_item[0] # 'G' from "Grilled"
second_char = grocery_item[8] # 'C' from "Chicken"
third_char = grocery_item[16] # 'S' from "Salad"

# Use negative indexing to find the last character of each word
# The index is calculated as -1 from the end of the string backwards
last_char1 = grocery_item[-1] # 'd' from "Salad"
last_char2 = grocery_item[-7] # 'n' from "Chicken"
last_char3 = grocery_item[-15] # 'd' from "Grilled"

# Testing
print("Length of item name:", length_of_item)
print("First character of each word:", first_char, second_char, third_char)
print("Last character of each word:", last_char1, last_char2, last_char3)
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
single

single

grocery_item = "Grilled Chicken Salad"




# Testing
print("Length of item name:", length_of_item)
print("First character of each word:", first_char, second_char, third_char)
print("Last character of each word:", last_char1, last_char2, last_char3)

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt