Python__24--格式化字符串与字符串的编码、解码转换

简介: 驻留机制、格式化字符串与字符串的编码、解码转换

1 格式化字符串

1.1 %占位符

  1. %s:字符串
  2. %d、%i:整数
  3. %f :浮点数
name='张三'
age=20
print('我叫%s,今年%d岁了'%(name,age))
#输出:我叫张三,今年20岁了

1.2 {}占位符

print('我叫{0},今年{1}岁了'.format(name,age))
print(f'我叫{name},今年{age}岁了')
#输出:我叫张三,今年20岁了
#输出:我叫张三,今年20岁了

1.3 单位精度

1.3.1 使用%

print('%d'%99)
print('%10d'%99)    #10代表输出宽度
print('%.3f'%3.1415926)    #.3f代表三位小数
print('%10.3f'%3.1415926)

1.3.2 使用{}

print('{0:.3f}'.format(3.1415926))    #0代表第一个占位符3.1415926,.3表示小数点后三位,f表示浮点型
print('{0:10.3f}'.format(3.1415926))    #10代表输出宽度

#输出:3.142
#输出:     3.142

1.4 对齐

# 左对齐
'{:*<10}'.format('分割线')
#输出:分割线*******
# 居中
'{:*^10}'.format('分割线')
#输出:***分割线****
# 右对齐
'{:*>10}'.format('分割线')
#输出:*******分割线

2 字符串的编码、解码转换

  • 编码:将字符串转换为二进制数据(bytes)
  • 解码:将bytes类型的数据转换成字符串类型
  • byte=s.encode(encoding='GBK') 编码
  • print(byte.decode(encoding='GBK')) 解码
  • byte=s.encode(encoding='UTF-8') 编码
  • print(byte.decode(encoding='UTF-8')) 解码

测试代码:

s="天涯共此时"
byte=s.encode(encoding='GBK')
print(byte)
print(byte.decode(encoding='GBK'))

byte=s.encode(encoding='UTF-8')
print(byte)
print(byte.decode(encoding='UTF-8'))

测试结果:

b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
天涯共此时
b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'
天涯共此时

进程已结束,退出代码为 0

3 字符串的驻留机制

测试代码:

a=257
b=257
c=257
print(a,id(a))
print(b,id(b))
print(c,id(c))

测试结果:

257 2494013466448
257 2494013466448
257 2494013466448

进程已结束,退出代码为 0

内存示意图:

https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ea0f5457052746f6830e6373618c9b89~tplv-k3u1fbpfcp-zoom-1.image

字符串驻留:

字符串驻留:是一种在内存中保存一份且不可变字符串的方法。(相同的字符串只保留一份)不同的值被存放在字符串的驻留池当中,Python的驻留机制对相同的字符串只保留一份拷贝,后续创建相同的字符串时,不会开辟新的空间,而是把字符串的地址赋给新的创建变量。

https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ac8409e99b094cfdaf76347778da6505~tplv-k3u1fbpfcp-zoom-1.image

import sys
a =sys.intern(b) #强制驻留

测试代码:

import sys
a='Python'
b='world'
c='Pythonworld'
d=a#+b
print(d,id(d))
print(c,id(c))
d=sys.intern(c)
print(d,id(d))
print(c,id(c))

测试结果:

Python 1828817256816
Pythonworld 1828817241584
Pythonworld 1828817241584
Pythonworld 1828817241584

进程已结束,退出代码为 0
相关文章
|
9天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
191 100
|
9天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
206 99
|
12天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
12天前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
12天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
23天前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
2月前
|
数据采集 存储 数据库
Python字符串全解析:从基础操作到高级技巧
Python字符串处理详解,涵盖基础操作、格式化、编码、正则表达式及性能优化等内容,结合实际案例帮助开发者系统掌握字符串核心技能,提升文本处理与编程效率。
168 0
|
Rust 算法 安全
【算法】1720. 解码异或后的数组(java / c / c++ / python / go / rust)
未知 整数数组 arr 由 n 个非负整数组成。 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。 给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。 请解码返回原数组 arr 。可以证明答案存在并且是唯一的。
|
13天前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
186 102
|
13天前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
186 104

推荐镜像

更多