Python簡明教程:基本概念

來源:互聯網
上載者:User

標籤:python

1 字面意義上的常量,如2、‘This is ok‘這樣的字串

>>> print(‘a,2,3‘)

a,2,3


2 字串

  • 單引號(‘)

    使用單引號指示字串,類似shell中的強引用,所有的空格、定位字元照原樣保留。

>>> print(‘This is ok‘)

This is ok


  • 雙引號(“)

    在雙引號中的字串與單引號中的字串使用完全相同

>>> print("This is ok")

This is ok


  • 三引號(‘‘‘)

    利用三引號,可以指示一個多行的字串,可以在三引號中自由的使用單引號和雙引號。

>>> ‘‘‘This is am string.This is the first line.

... This is the second line.

... "What‘s your name?" Lasked.

... He said "Bond,JamesBond."‘‘‘

‘This is am string.This is the first line.\nThis is the second line.\n"What\‘s your name?" Lasked.\nHe said "Bond,JamesBond."‘


  • 轉義符

>>> ‘what \‘s your name‘            #在單引號中實現單引號的使用

"what ‘s your name"

或使用雙引號

>>> "what‘your name"

"what‘your name"

>>> "This is \"ok\","               #雙引號中實現雙引號的引用

‘This is "ok",‘

備忘:行尾單獨反斜線,表示字串在下一行繼續,如:

>>> ‘This is ok\

... ,hello‘

‘This is ok,hello‘


  • 自然字串

    如果不需要轉義符那樣特別處理的字串,那麼可以給字串加上首碼r或R來指定一個自然字串

>>> r‘New line are by \n‘

‘New line are by \\n‘



本文出自 “一萬年太久,只爭朝夕” 部落格,請務必保留此出處http://zengwj1949.blog.51cto.com/10747365/1925662

Python簡明教程:基本概念

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.