Python 基礎之字串string詳解及執行個體,pythonstring

來源:互聯網
上載者:User

Python 基礎之字串string詳解及執行個體,pythonstring

Python字串(string) 詳解 及 代碼

Python的字串可以使用單引號('), 雙引號("), 三引號('''); 三引號(''')裡面, 可以添加單引號和雙引號, 也可以通過逸出序列(\)添加;
字串放在一起自動連接成為一個字串;

字串前面添加限定詞R或r, 表示是自然字串(nature string), 可以忽略裡面的格式限制;

在物理行末尾添加"\", 可以串連下一個物理行; 括弧, 方括弧, 大括弧也可以一定限度的擴充物理行;

具體參見代碼注釋;

代碼如下:

# -*- coding: utf-8 -*-  #==================== #File: abop.py #Author: Wendy #Date: 2013-12-03 #====================  #eclipse pydev, python3.3  #三引號可以自由的使用雙引號("")和單引號('') s = ''''' I am a girl and like heels. Hello, "python" sister. '''  #逸出序列"\" st = '''''Hello, girls, l like (\'''). '''  #字串放在一起自動連接 sa = 'Girls are ''the best. '  #r表示自然字串, 不會進行轉義(\n) sr = r"nature is good. \n {0}"  #"\"表示連接字串 sc = 'Oh, the lines are too \ large'  #括弧, 方括弧, 大括弧, 可以限定範圍, 不用使用轉義 print("the braces can do {thing}.".    format(thing="lady"))  print(s) print(st) print(sa) print(sr) print(sc) 

輸出:

the braces can do lady.  I am a girl and like heels. Hello, "python" sister.  Hello, girls, l like (''').  Girls are the best.  nature is good. \n {0} Oh, the lines are too large 

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.