Lo slicing e la concatenazione sono due tecniche fondamentali per manipolare stringhe in Python.
Sono strumenti essenziali quando devi analizzare, riorganizzare o presentare dati testuali.
Lo slicing utilizza la sintassi:
string[start:end]
Esempio:
fruit = "Strawberries"
# Slicing the string to get "Straw"
sliced_fruit = fruit[0:5] # da S (indice 0) a w (indice 4)
print("Sliced part:", sliced_fruit)
La concatenazione unisce più stringhe usando l'operatore +.
part1 = "Straw"
part2 = "berry"
new_word = part1 + part2
print("Concatenated word:", new_word)
# Aggiungere uno spazio
print(part1 + " " + part2)
Lavorerai con una stringa contenente un elenco di alimenti. Usa slicing e concatenazione per estrarre alcune parole e costruire una frase.
grocery_items = "milk, eggs, cheese, bread, apples"
"milk" → dairy1"cheese" → dairy2"bread" → bakery1(Hai piena libertà nel determinare gli indici corretti.)
We have dairy and bakery items: <dairy1>, <dairy2>, and <bakery1> in aisle 5
grocery_items = "milk, eggs, cheese, bread, apples"
# Extracting items using slicing
dairy1 = grocery_items[0:4] # "milk"
dairy2 = grocery_items[12:18] # "cheese"
bakery1 = grocery_items[20:25] # "bread"
# Concatenating the final message
message = "We have dairy and bakery items: " + dairy1 + ", " + dairy2 + ", and " + bakery1 + " in aisle 5"
print(message)