[Python] python 字典 雙引號或單引號 - 討論區

[Python] python 字典 雙引號或單引號

文章瀏覽次數 1237 文章回覆數 3

coscell

coscell圖像(預設)

2021-09-02 16:47:51

From:123.193.252.152

python 用一對雙引號 (") 或單引號 (') 包夾的內容稱為字串 (string).

>>> "abc"
'abc'
>>> type("abc")
<class 'str'>
>>> '大海'
'大海'
>>> type('大海')
<class 'str'>

以上兩種用法效果完全相同。
如果包夾的內容為空(不是空白而是沒有內容像這樣 "", '')就是空字串。

如果字串內容本身含有與外包的相同引號就需要特別處理,否則會有語法錯誤:

>>> 'I'm here!'
  File "<stdin>", line 1
    'I'm here!'
       ^
SyntaxError: invalid syntax

處理方法有二:

  1. 把外包的引號改為另外一種:
>>> "I'm here!"
"I'm here!"
  1. 把內容的引號用跳脫字元 \ 跳開:
>>> 'I\'m here!'
"I'm here!"

留言

#1

cello

cello圖像(預設)

2021-09-02 18:02:01

From:61.64.210.55

話說nvda在cmd底下對於中文字的顯示訊息仍舊無解

>>print('大海')
大大海海
有沒什麼好方法能夠解決這問題

#2

coscell

coscell圖像(預設)

2021-09-02 19:34:24

From:123.193.252.152

把命令視窗的自行改為點陣自行

#3

cello

cello圖像(預設)

2021-09-02 20:09:24

From:61.64.210.55

真的行了,太棒了