3.2 数据类型

列表

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。

定义列表

names = ['zhangsan','lisi','wangwu']

通过下标访问列表中的元素,下标从0开始计数

print(name[0])

运行结果为:

zhangsan

取多个元素

names[1:4:2]  #取下标1至下标4之间的数字,每隔一个元素,就取一个,包括1,不包括4

追加

names.append('xiaoliu')

插入

names.insert(1, "aa")

运行结果为:

['zhangsan', 'aa', 'lisi', 'wangwu', 'xiaoliu']

修改

删除

按下标删除

按指定元素删除

删除列表中最后一个值

扩展

输出结果为:

['zhangsan', 'lisi', 'wangwu', '1', '2']

拷贝

统计

输出结果为:

3

排序

输出结果为:

[1, 2, 2, 2, 3, 3, 4, 4]

翻转

获取下标

输出结果为:

2

注意:只返回找到的第一个下标

元组

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

输出结果为:

('zhangsan', 'lisi', 'wangwu') 1 1

字符串

特性:不可修改

首字母大写

输出结果为:

Abc

大写全部变小写

输出结果为:

abc

居中

输出结果为:

-----------------------ABC------------------------

统计lex出现次数

输出结果为:

2

将字符串编码成bytes格式

判断字符串是否已Li结尾

输出结果为:

True

输出'Wangzhi Clay', 将\t转换成多长的空格

输出结果为:

Wangzhi Clay

find 查找A,找到返回其索引, 找不到返回-1

输出结果为:

5

format 格式化输出

输出结果为:

my name is clay, and age is 18

输出结果为:

my name is 18, and age is clay

输出结果为:

my name is clay, and age is 18

format_map 格式化输出

输出结果为:

my name is clay, and age is 18

index 返回索引

输出结果为:

1

isalnum都是字母和数字

输出结果为:

True

isdigit 是否为整数

输出结果为:

True

isnumeric isprintable isspace isupper ????

join

输出结果为:

alex|jack|rain

maketrans 替换

输出结果为:

th3s 3s str3ng 2x1mpl2 ... w4w!!

partition分隔

输出结果为:

('th', 'is', ' is string example ... wow!!')

swapcase 大小写互换

输出结果为:

XIAOXIEdaxie

zfill 用0补齐

输出结果为:

00000000xiaoxieDAXIE

ljust 靠左侧补齐

输出结果为:

xiaoxieDAXIE--------

rjust 靠右侧补齐

输出结果为:

--------xiaoxieDAXIE

isidentifier 检测一段字符串可否被当作标志符,即是否符合变量命名规则

False

Last updated

Was this helpful?