Python的字符串可以使用單引號('), 雙引號("), 三引號('''); 三引號(''')里面, 可以添加單引號和雙引號, 也可以通過轉(zhuǎn)義序列(\)添加;
字符串放在一起自動連接成為一個(gè)字符串;
字符串前面添加限定詞R或r, 表示是自然字符串(nature string), 可以忽略里面的格式限制;
在物理行末尾添加"\", 可以連接下一個(gè)物理行; 括號, 方括號, 大括號也可以一定限度的擴(kuò)充物理行;
具體參見代碼注釋;
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# -*- 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. ''' #轉(zhuǎn)義序列"\" st = '''''Hello, girls, l like (\''' ). ''' #字符串放在一起自動連接 sa = 'Girls are ' 'the best. ' #r表示自然字符串, 不會進(jìn)行轉(zhuǎn)義(\n) sr = r "nature is good. \n {0}" #"\"表示連接字符串 sc = 'Oh, the lines are too \ large' #括號, 方括號, 大括號, 可以限定范圍, 不用使用轉(zhuǎn)義 print ( "the braces can do {thing}." . format (thing = "lady" )) print (s) print (st) print (sa) print (sr) print (sc) |
輸出:
1
2
3
4
5
6
7
|
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 |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/caroline_wendy/article/details/17113225