Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Virgolette | Manipolazione delle Stringhe in Python
Tipi di Dati in Python

book
Virgolette

Le stringhe possono essere racchiuse tra virgolette singole ('...') o virgolette doppie ("..."). Tuttavia, se hai bisogno di usare le virgolette all'interno della tua stringa, devi fare attenzione per evitare errori di sintassi.

# This will raise a syntax error due to the apostrophe inside single quotes
transaction_note = 'Today's payment was delayed'
print(transaction_note)
123
# This will raise a syntax error due to the apostrophe inside single quotes transaction_note = 'Today's payment was delayed' print(transaction_note)
copy

Python interpreta l'apostrofo ' in "Today's" come la fine della stringa, il che causa confusione nel resto della linea.

Modi corretti per usare le virgolette all'interno delle stringhe

Usa le virgolette doppie all'esterno se la stringa contiene un apostrofo:

# Using double quotes allows us to include an apostrophe safely
transaction_note = "Today's payment was delayed"
print(transaction_note)
123
# Using double quotes allows us to include an apostrophe safely transaction_note = "Today's payment was delayed" print(transaction_note)
copy

Usa le virgolette singole all'esterno se la stringa contiene virgolette doppie:

# Using single quotes to include a quote in the text
audit_remark = 'The client said: "We will send the invoice tomorrow."'
print(audit_remark)
123
# Using single quotes to include a quote in the text audit_remark = 'The client said: "We will send the invoice tomorrow."' print(audit_remark)
copy

Usa le triple virgolette per includere entrambi i tipi di virgolette

Le triple virgolette ('''...''' o """...""") sono spesso utilizzate per il testo su più righe, ma sono anche utili quando una stringa contiene sia virgolette singole che doppie:

# Triple quotes allow both single and double quotation marks
financial_summary = """Today's report includes the note: "Check the 'Q1' revenue drop." """
print(financial_summary)
123
# Triple quotes allow both single and double quotation marks financial_summary = """Today's report includes the note: "Check the 'Q1' revenue drop." """ print(financial_summary)
copy

Esempio Pratico di Contabilità

# Correct use of quotes to log an accountant's comment
comment = "The accountant's note: 'Double-check the tax deduction before approval.'"
print(comment)

# Using triple quotes to format a longer, multi-line report comment
report_comment = """
Manager's instructions:
- Review the 'Accounts Receivable' section.
- Confirm with the accountant: "Is the write-off policy still valid?"
"""
print(report_comment)
1234567891011
# Correct use of quotes to log an accountant's comment comment = "The accountant's note: 'Double-check the tax deduction before approval.'" print(comment) # Using triple quotes to format a longer, multi-line report comment report_comment = """ Manager's instructions: - Review the 'Accounts Receivable' section. - Confirm with the accountant: "Is the write-off policy still valid?" """ print(report_comment)
copy

Questo approccio garantisce che le tue stringhe siano sintatticamente corrette e migliora la leggibilità del codice, specialmente quando si lavora con dati finanziari reali che possono includere citazioni nella documentazione o nei commenti.

Compito

Swipe to start coding

La frase seguente contiene sia apici singoli che testo normale. Per renderla una stringa valida in Python, aggiungi le virgolette corrette intorno alla frase. Puoi utilizzare uno dei metodi descritti in precedenza (doppi apici all'esterno, o tripli apici).

Soluzione

# Put appropriate quotation marks here
string = "The accountant said, 'Please check the tax report by Monday'"

print(string)
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 2
single

single

# Put appropriate quotation marks here
string = ___The accountant said, 'Please check the tax report by Monday'___

print(string)

Chieda ad AI

expand

Chieda ad AI

ChatGPT

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

some-alt