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
World')   #换行
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 #
原文地址:https://www.cnblogs.com/cmnz/p/6946136.html