课程: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])