Python_駐留機制


 1 #coding=utf-8
 2 #coding:utf-8
 3 #- * -coding:utf-8 - * -
 4 
 5 '''以上為注明字符串的編碼格式'''
 6                                        #駐留機制
 7 '''Python支持短字符串駐留機制,對於短字符串,將其賦值給多個不同的對象時,內存中只有一個副本,多個對象共享該副本,
 8 與其他類型數具有相同的特點。然而這一特點並不適用於長字符串,長字符串不遵守駐留機制'''
 9 a='1234'
10 b='1234'
11 print(id(a)==id(b)) #短字符串
12 #True
13 a='1234' * 50   #長字符串
14 b='1234' * 50
15 print(id(a)==id(b))
16 #False
17 #判斷是否為字符串,可使用內置方法isinstace()或type()
18 print(type('字符串'))
19 #<class 'str'>
20 print(type('字符串'.encode('gbk')))
21 #<class 'bytes'>
22 print(bytes)
23 #<class 'bytes'>
24 print(isinstance('中國',str))
25 #True
26 print(type('中國')==str)
27 #True
28 print(type('字符串'.encode())==bytes)
29 #True
30 print(type('字符串')==bytes)
31 #True
32 
33 
34 #轉義字符的使用
35 print('Hello\nWorld')   #換行
36 # Hello
37 # World
38 print(oct(65))  #轉換成8進制
39 # 0o101
40 print('\101')   #3位8進制數對應的字符
41 # A
42 print('\x41')   #2位十六進制數對應的字符
43 # A
44 print(ord('')) #以一個字符(長度為1的字符串)作為參數,返回對應的ASCII數值,或者Unicode數值,如果所給的Unicode字符超出了你的Python定義范圍,則會引發一個TypeError的異常
45 # 24352
46 print(hex(15))  #轉換一個整數對象為十六進制的字符串表示
47 print('\u8464')
48 #

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM