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
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答