python12-字段串操作

news/2025/2/8 12:48:02/

课程:B站大学
记录python学习,直到学会基本的爬虫,使用python搭建接口自动化测试就算学会了,在进阶webui自动化,app自动化

字符串那些事儿

  • 字符串转换类
  • 字符串对齐类
  • 字符串去除空白类
  • 字符串分割类
  • 字符串连接类
  • 编码解码类
  • 切片操作
    • 实践是检验真理的唯一标准


# 字符串判断类
  • startswith() 检查字符串是否是以 prefix 开头,是则返回 True,否则返回 False。如果 start 和 end指定值,则在指定范围内检查.
    重点掌握
    格式: startswith(prefix, start, end)
python">url = "https://www.ceshiren.com"
print(url.startswith("https://"))
print(url.startswith("https://", 0, 3))
print(url.startswith("https://", 5, 30))
  • endswith() 检查字符串是否是以 suffix 结束,是则返回 True,否则返回 False。如果 start 和 end指定值,则在指定
    范围内检查.
    重点掌握
    格式: endswith(suffix, start, end)
python">url = "https://www.ceshiren.com"
print(url.endswith(".com"))
print(url.endswith(".com", 0, 20))
print(url.endswith(".com", 5, 30))
  • isalpha() 如果 string 至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False

重点掌握
格式: isalpha()

python">print("abc".isalpha())
print("ABC".isalpha())
print("ABCabc".isalpha())
print("123".isalpha())
print("a b".isalpha())
print("abc123".isalpha())
print("123abc".isalpha())
print("a@".isalpha())
print("".isalpha())
  • isdigit() 如果 string 只包含数字则返回 True 否则返回 False.

重点掌握
格式: isdigit()

python">print("123".isdigit())
print("123abc".isdigit())
print("abc123".isdigit())
print("".isdigit())
  • isalnum() 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True ,否则返回 False

格式: isalnum()

python">print("abc".isalnum())
print("ABC".isalnum())
print("ABCabc".isalnum())
print("123".isalnum())
print("abc123".isalnum())
print("123abc".isalnum())
print("a b".isalnum())
print("a@".isalnum())
print("".isalnum())
  • isspace() 如果 string 中只包含空格,则返回 True,否则返回 False.

格式: isspace()

python">print(" ".isspace())
print("    ".isspace()) # tab键,由4个空白组成
print("\t".isspace())
print("\n".isspace())
print("\r".isspace())
print("".isspace())
print(" a".isspace())
print("1 ".isspace())
  • isupper() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回True,否则返回 False
    重点掌握
    格式: isupper()
python">print("ABC".isupper())
print("ABC123".isupper())
print("123ABC".isupper())
print("A!@#B".isupper())
print("abc".isupper())
print("abC".isupper())
print("abc123".isupper())
print("Abc!@#".isupper())
print("123".isupper())
print("".isupper())
print(" ".isupper())
  • islower() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回
    True,否则返回 False
    重点掌握
    格式: islower()
python">print("abc".islower())
print("abc123".islower())
print("ABC".islower())
print("abC".islower())
print("Abc!@#".islower())
print("123".islower())
print("".islower())
print(" ".islower())
  • istitle() 如果 string 是标题化的(所有单词首字符是大写)则返回 True,否则返回 False

格式: istitle()

python">print("Username".istitle())
print("User Name".istitle())
print("User_Name".istitle())
print("User.Name".istitle())
print("User+Name".istitle())
print("username".istitle())
print("UserName".istitle())
print("user name".istitle())
print("User name".istitle())

字符串转换类

  • capitalize() 把字符串的第一个字符大写

格式: capitalize()

python">print("username".capitalize())
print("Username".capitalize())
print("userNAME".capitalize())
print("this is username".capitalize())
  • title() 返回标题化的 string,就是说所有单词都是以大写开始,其余字母均为小写

格式: title()

python">print("this is username".title())
print("THIS IS USERNAME".title())
print("tHIS IS username".title())
  • upper() 转换 string 中的小写字母为大写
    重点掌握
    格式: upper()
python">print("abc".upper())
print("ABC".upper())
print("abCd".upper())
print("abc123".upper())
print("abc123ABC".upper())
  • lower() 转换 string 中的小写字母为小写

重点掌握
格式: lower()

python">print("abc".lower())
print("ABC".lower())
print("abCd".lower())
print("abc123".lower())
print("abc123ABC".lower())

字符串对齐类

  • center() 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串,如果指定 fillchar
    参数,则使用指定字符填充,fillchar 参数长度只能为 1

格式: center(width, fillchar)

python">print("|"+"hogworts".center(20) + "|")
print("|"+"hogworts".center(5) + "|")
print("|"+"hogworts".center(20, "-") + "|")
  • ljust() 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串,如果指定 fillchar
    参数,则使用指定字符填充,fillchar 参数长度只能为 1

格式: ljust(width, fillchar)

python">print("|"+"hogworts".ljust(20) + "|")
print("|"+"hogworts".ljust(5) + "|")
print("|"+"hogworts".ljust(20, "-") + "|")
  • rjust() 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串,如果指定 fillchar
    参数,则使用指定字符填充,fillchar 参数长度只能为 1

格式: rjust(width, fillchar)

python">print("|"+"hogworts".rjust(20) + "|")
print("|"+"hogworts".rjust(5) + "|")
print("|"+"hogworts".rjust(20, "-") + "|")

字符串去除空白类

  • strip() 删除 string 左右两侧的空白字符, 如果指定 chars 参数,则删除左右两侧指定的字符

重点掌握

格式: strip(chars)

python">print("|" + "  hogworts  " + "|")
print("|" + "  hogworts  ".strip() + "|")
print("|" + "  hogworts".strip() + "|")
print("|" + "hogworts  ".strip() + "|")
print("|" + "  h o g w o r t s  ".strip() + "|")
print("|" + "bachogwortsabc".strip("cba") + "|")
  • lstrip() 删除 string 左边的空白字符, 如果指定 chars 参数,则删除左侧指定的字符

格式: lstrip(chars)

python">print("|" + "  hogworts  " + "|")
print("|" + "  hogworts  ".lstrip() + "|")
print("|" + "  hogworts".lstrip() + "|")
print("|" + "hogworts  ".lstrip() + "|")
print("|" + "  h o g w o r t s  ".lstrip() + "|")
print("|" + "bachogwortsabc".lstrip("cba") + "|")
  • rstrip() 删除 string 右边的空白字符, 如果指定 chars 参数,则删除右侧指定的字符

格式: rstrip(chars)

python">print("|" + "  hogworts  " + "|")
print("|" + "  hogworts  ".rstrip() + "|")
print("|" + "  hogworts".rstrip() + "|")
print("|" + "hogworts  ".rstrip() + "|")
print("|" + "  h o g w o r t s  ".rstrip() + "|")
print("|" + "bachogwortsabc".rstrip("cba") + "|")

字符串分割类

  • split() 以 sep 为分隔符分割 string ,如果指定 maxsplit 参数,则仅分割 maxsplit 次
    重点掌握
    格式: split(sep, maxsplit)
python">print("a-b-c-d".split("-"))
print("a-b-c-d".split("-", 2))
print("a--b-c-d".split("-"))
print("a-+b-c-d".split("-+"))
print("a b\tc\nd\re".split())
print("a b c d e".split(" ", 3))
  • splitlines() 使用换行符\n分割 string,如果指定 keepends 参数,则结果中会保留\n符号
    格式: splitlines(keepends)
python">print("a\nb\nc".splitlines())
print("a\nb\nc".splitlines(True))
  • partition() 从 sep 出现的第一个位置起,把 string 分成一个 3 元素的元组
    (string_pre_sep,sep,string_post_sep), 如果 string 中不包含 sep 则
    string_pre_str == string,其余元素为空字符串
    格式: partition(sep)
python">print("This is Hogworts".partition("is"))
print("This is Hogworts".partition("iss"))
  • rpartition() 从右向左 sep 出现的第一个位置起,把 string 分成一个 3 元素的元组
    (string_pre_sep,sep,string_post_sep),如果 string 中不包含 sep 则
    string_post_str == string,其余元素为空字符串

格式: rpartition(sep)

python">print("This is Hogworts".rpartition("is"))
print("This is Hogworts".rpartition("iss"))

字符串连接类

  • +号 将两个字符串连接生成一个新字符串, + 号两侧必须都是字符串

重点掌握

格式: str1 + str2

python">print("Hello" + "World")
print("Hello" + "123")
print("Hello" + 123)
  • *号 将字符串重复 N 次后生成一个新字符串

格式: str * n

python">print("*"* 10)
print("hello"* 10)
  • join() 使用 string 连接可迭代对象中的所有元素,可迭代对象参数中的所有元素必须是字符串

重点掌握

格式: join(iterable)

python">print("".join(("a","b","c")))
print("-".join(("a","b","c")))
print("->".join(("a","b","c")))
print("->".join(["a","b","c"]))
print("->".join({"a","b","c"}))
print("->".join({"a":"A","b":"B","c":"C"}))

编码解码类

  • encode() 使用 encoding 指定的字符集,对 string 进行编码,转换成二进制字符串

格式: encode(encoding)

python">print("abc123".encode("gbk"))
print("你好".encode("gbk"))print("abc123".encode("utf-8"))
print("你好".encode("u8"))
  • decode() 使用 encoding 指定的字符集,对 string 进行解码,转换成字符串对象, string 必须是二进制字符串

格式: decode(encoding)

python">s1 = b'\xc4\xe3\xba\xc3'
s2 = b'\xe4\xbd\xa0\xe5\xa5\xbd'
print(s1.decode("gbk"))
print(s2.decode("utf-8"))# print(s1.decode("u8"))
# print(s2.decode("gbk"))

切片操作

  • 对字符串按指定的范围进行截取,得到一个子字符串,指定范围时,起始下标必须小于结束下标,且子字符串不包含结束下标

重点掌握

格式: str[start: end: step]

python">s = "abcdefg"# 普通切片
print(s[0: 2])
# 省略范围
print(s[0:])
print(s[: 2])
print(s[:])
# 指定步长
print(s[::1])
print(s[::2])
# 负下标
print(s[-3: -1])
# 负步长
print(s[-1: -3: -1])
# 逆序
print(s[::-1])

在这里插入图片描述

实践是检验真理的唯一标准


http://www.ppmy.cn/news/1570321.html

相关文章

JUC常见类

JUC是java.util.concurrent的简称,当中有我们需要熟知的常见类,此文为你带来相关知识 前面我们学习了3种创建线程的方法,分别是: 继承Thread类,重写run方法实现Runnable接口,重写run方法创建线程池&#x…

阿里云平台使用 DeepSeek 模型:完整开发指南

本文将详细介绍如何在阿里云平台上使用 DeepSeek 大语言模型,包括环境配置、基础调用、高级特性以及最佳实践等内容。 一、入门准备 1.1 环境配置 首先需要配置必要的开发环境: # 安装阿里云 DashScope SDK pip install alibabacloud_dashscope# 安装其他依赖包 pip insta…

CSS 伪类(Pseudo-classes)的详细介绍

CSS 伪类详解与示例 在日常的前端开发中,CSS 伪类可以帮助我们非常精准地选择元素或其特定状态,从而达到丰富页面表现的目的。本文将详细介绍以下伪类的使用: 表单相关伪类 :checked、:disabled、:enabled、:in-range、:invalid、:optional、…

CRM系统中的数据分析和报表功能如何帮助企业?

CRM系统中的数据分析和报表功能:企业战略决策的得力助手 在当今竞争激烈的商业环境中,企业要想保持竞争力并实现持续增长,必须依靠精准的数据分析来制定有效的战略决策。而客户关系管理(CRM)系统的数据分析与报表生成…

Canny 边缘检测

步骤 1.降噪 应用高斯滤波器,以平滑图像,滤除噪声。 边缘检测易受噪声影响,所以使用高斯滤波器平滑图像,降低噪声。 2.梯度 计算图像中每个像素点的梯度大小和方向。 计算大小 Sobel算子是一种常用的边缘检测滤波器&#xff…

深度剖析 C++17 中的 std::byte:解锁字节级编程新境界

文章目录 一、引入背景二、基本定义三、特性详解不可隐式转换为整型显式转换为unsigned char位运算支持字面量支持四、使用场景内存操作数据序列化与反序列化网络通信文件读写操作五、与其他数据类型的交互与字符类型的交互与整数类型的交互与指针类型的交互六、注意事项避免混…

网络原理一>数据链路层协议->以太网协议

目录 以太网协议的结构:类型:ARP请求应答报文:CRC:MTU: 为什么需要mac地址:mac地址和IP地址的区别: 以太网协议的结构: 以太网是数据链路层和物理层的主要协议 源IP,目的IP就不多说…

Day38-【13003】短文,二叉树,完全二叉树,二叉树的顺序存储,和链式存储

文章目录 第二节 二叉树二叉树的定义及重要性质n个结点,能组合成多少个不同的二叉树满二叉树、完全二叉树完全二叉树的性质二叉树的性质二叉树的结点数完全二叉树的高度 二叉树的存储顺序存储方式链式存储方式二叉链表的程序实现二叉链表空指针域计算 第二节 二叉树…