字符串

切片:

  • 切片 方法适用于 字符串列表元组

    • 切片 使用 索引值 来限定范围,从一个大的 字符串 中 切出 小的 字符串

    • 列表 和 元组 都是 有序 的集合,都能够 通过索引值 获取到对应的数据

    • 字典 是一个 无序 的集合,是使用 键值对 保存数据

---拆分和连接

string.split(str="", num)     以 str 为分隔符拆分 string,如果 num 有指定值,则仅分隔 num + 1 个子字符串,str 默认包含 '\r', '\t', '\n' 和空格

string.join(seq)     以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

 

---去除空白字符

string.strip()     截掉 string 左右两边的空白字符

 

---查找和替换

string.startswith(str)    检查字符串是否是以 str 开头,是则返回 True

string.find(str, start=0, end=len(string))     检测 str 是否包含在 string 中,如果 start 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回 -1

string.index(str, start=0, end=len(string))     跟 find() 方法类似,不过如果 str 不在 string 会报错

string.replace(old_str,new_str,num=string.count(old))     把 string 中的 old_str 替换成 new_str,如果 num 指定,则替换不超过 num 次

 

列表:

 

序号

分类

关键字 / 函数 / 方法

说明

1

增加

列表.insert(索引, 数据)

在指定位置插入数据

列表.append(数据)

在末尾追加数据

列表.extend(列表2)

将列表2 的数据追加到列表

2

修改

列表[索引] = 数据

修改指定索引的数据

3

删除

del 列表[索引]

删除指定索引的数据

列表.remove[数据]

删除第一个出现的指定数据

列表.pop

删除末尾数据

列表.pop(索引)

删除指定索引数据

列表.clear

清空列表

4

统计

len(列表)

列表长度

列表.count(数据)

数据在列表中出现的次数

5

排序

列表.sort()

升序排序

列表.sort(reverse=True)

降序排序

列表.reverse()

逆序、反转

列表、字典图解:

 

列表:

python高级变量

 

字典:

python高级变量

 

 

 

 

 

 

相关文章:

  • 2022-01-21
  • 2022-12-23
  • 2021-07-14
  • 2021-10-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-26
  • 2021-09-18
  • 2021-12-18
  • 2022-03-03
  • 2022-12-23
  • 2021-07-25
相关资源
相似解决方案