Python 数据类型之字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 数据类型之字符串相关的知识,希望对你有一定的参考价值。
Python 数据类型之字符串
格式化字符串
参数替换(类的对象,方法调用,返回一个字符串)
>>> username = ‘root‘
>>> password = ‘foxconn168!‘
>>> "{0}‘s password is {1}".format(username,password) --> 类的对象方法调用
"root‘s password is foxconn168!" --> 返回一个字符串
复合字段名
使用列表作为参数,可通过列表下标索引访问其元素
使用字典作为参数,可通过键来访问键值
使用模块作为参数,可通过名字访问变量和函数
使用类作为参数,可通过名字访问方法和属性
以上参数可任意组合
>>> a_list = [1,2,3,4,5,6] --> 使用列表作为参数
>>> ‘6 * {0[0]} = {0[5]}‘.format(a_list) --> {0[0}] 第一个参数的第一个元素
‘6 * 1 = 6‘
>>>
>>> a_dict = {Name:Sam,Age:18} --> 使用字典作为参数
>>> "{0[Name]}‘s age is {0[Age]}".format(a_dict) --> {0[0}] 第一个参数的第一个元素
"Sam‘s age is 18"
格式说明符
‘{0:.1f} {1}‘.format(size, suffix)
--> {0:0.1f} 0 为第一个参数;:为格式说明符;.1f为四舍五入到1位
--> {1} 为第二个参数
>>> a_dict
{‘Name‘: ‘Sam‘, ‘Age‘: 18, ‘heigh‘: 65.55}
>>> "{0[Name]}‘s heigh is {0[heigh]:.1f}KG.".format(a_dict)
"Sam‘s heigh is 65.5KG."
其他字符串操作
s.splitlines() --> 按行区分
s.lower() --> 小写字符转换
s.count() --> 字符个数统计
>>> s = ‘‘‘Instance attribute dictionaries are avoided by creating
... class-level descriptors that intercept slot name access,and map
... those names to sequential storage apace in the instance.‘‘‘
>>> s.splitlines() --> 按行区分,返回列表
[‘Instance attribute dictionaries are avoided by creating ‘, ‘class-level descriptors that intercept slot name access,and map‘, ‘those names to sequential storage apace in the instance.‘]
>>> s.lower() --> 小写字符转换
‘instance attribute dictionaries are avoided by creating
class-level descriptors that intercept slot name access,and map
those names to sequential storage apace in the instance.‘
>>> print(s.lower())
instance attribute dictionaries are avoided by creating
class-level descriptors that intercept slot name access,and map
those names to sequential storage apace in the instance.
>>> s.lower().count(‘s‘) --> 字符‘s‘出现的个数
14
>>>
字符串转换为字典
>>> info = ‘name - sam / program - python / time - 2018‘
>>> info_list = info.split(‘ / ‘)
>>> print(info_list)
[‘name - sam‘, ‘program - python‘, ‘time - 2018‘]
>>> in_info_list = [x.split(‘ - ‘) for x in info_list]
>>> print(in_info_list)
[[‘name‘, ‘sam‘], [‘program‘, ‘python‘], [‘time‘, ‘2018‘]]
>>> a_dict = dict(in_info_list)
>>> print(a_dict)
{‘name‘: ‘sam‘, ‘program‘: ‘python‘, ‘time‘: ‘2018‘}
字符串分片
类似列表分片,返回一个新的字串
>>> a_string = ‘Beginning Python:using Python 3.x.‘
>>> a_string[0:5]
‘Begin‘
>>> a_string[:10]
‘Beginning ‘
>>> a_string[5:-5]
‘ning Python:using Python‘
>>> a_string[:-5]
‘Beginning Python:using Python‘
>>> a_string[9:]
‘ Python:using Python 3.x.‘
string VS bytes
string:一个不可变(immutable)的unicode编码的字符序列
bytes:一串由0 - 255 之间的数字序列组成的bytes 对象(ASCII 或是x00 - xff)
bytes 对象是不可变的,可将bytes 对象转换为bytearray对象进行赋值(必须为0到255中的整数)
可通过 + 连接bytes对象
可通过下标取值(返回一个0到255中的整数)或len()计算bytes对象长度
>>> bt = b‘acd‘ --> bytes 定义
>>> type(bt)
<class ‘bytes‘>
>>> bt = b‘acd‘ + b‘x98‘ --> bytes 使用 + 连接
>>> bt
b‘acdx98‘
>>> bt = b‘acd‘ + b‘b‘
>>> bt
b‘acdb‘
>>> bt = bt + b‘x99‘
>>> bt
b‘acdbx99‘
>>> len(bt) --> bytes 对象长度计算
5
>>> bt[0] --> 通过索引下标取bytes对象的值
97
>>> bt[0] = 100 --> 不能直接对bytes对象赋值
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘bytes‘ object does not support item assignment
>>> btrr = bytearray(bt) --> 将bytes对象转换为bytearray对象
>>> btrr[0]
97
>>> btrr[0] = 100 --> 对bytearray对象直接赋值
>>> btrr
bytearray(b‘dcdbx99‘)
>>> bt
b‘acdbx99‘
>>> type(btrr) --> bytearray对象的类型
<class ‘bytearray‘>
>>> len(btrr)
5
>>> type(btrr)
<class ‘bytearray‘>
string 和bytes 相互转换
string encode bytes
bytes decode string
>>> a_string = ‘學習python‘
>>> len(a_string)
8
>>> by = a_string.encode(‘utf-8‘)
>>> by
b‘xe5xadxb8xe7xbfx92python‘
>>> len(by)
12
>>> by = a_string.encode(‘big5‘)
>>> by
b‘xbexc7xb2xdfpython‘
>>> len(by)
10
>>> roundtrip = by.decode(‘big5‘)
>>> roundtrip
‘學習python‘
>>> roundtrip == a_string
True
以上是关于Python 数据类型之字符串的主要内容,如果未能解决你的问题,请参考以下文章