- The Python Apprentice
- Robert Smallshire Austin Bingham
- 136字
- 2021-07-02 22:16:56
String quoting styles
Literal strings in Python are delimited by quotes:
>>> 'This is a string'
You can use single quotation marks, as we have above. Or you can use double quotation marks, as shown below:
>>> "This is also a string"
You must, however, be consistent. For example, you can't use a double quotation mark paired with a single quotation mark:
>>> "inconsistent'
File "<stdin>", line 1
"inconsistent'
^
SyntaxError: EOL while scanning string literal
Supporting both quoting styles allows you to easily incorporate the other quote character into the literal string without resorting to ugly escape character gymnastics:
>>> "It's a good thing."
"It's a good thing."
>>> '"Yes!", he said, "I agree!"'
'"Yes!", he said, "I agree!"'
Notice that the REPL exploits the same quoting flexibility when echoing the strings back to us.
推薦閱讀
- Java程序設(shè)計(jì)與開(kāi)發(fā)
- Spring Cloud Alibaba微服務(wù)架構(gòu)設(shè)計(jì)與開(kāi)發(fā)實(shí)戰(zhàn)
- 跟小海龜學(xué)Python
- Python進(jìn)階編程:編寫更高效、優(yōu)雅的Python代碼
- 實(shí)戰(zhàn)低代碼
- Raspberry Pi Home Automation with Arduino(Second Edition)
- C語(yǔ)言程序設(shè)計(jì)
- Python機(jī)器學(xué)習(xí):預(yù)測(cè)分析核心算法
- Python爬蟲(chóng)、數(shù)據(jù)分析與可視化:工具詳解與案例實(shí)戰(zhàn)
- Oracle GoldenGate 12c Implementer's Guide
- Rust游戲開(kāi)發(fā)實(shí)戰(zhàn)
- C++ Fundamentals
- HTML+CSS+JavaScript網(wǎng)頁(yè)制作:從入門到精通(第4版)
- Keil Cx51 V7.0單片機(jī)高級(jí)語(yǔ)言編程與μVision2應(yīng)用實(shí)踐
- C語(yǔ)言編程魔法書(shū):基于C11標(biāo)準(zhǔn)