文章目录
大小写函数
- upper()函数:将字符串中的所有小写字母转换为大写字母。isupper():判断是否大写
s = "hello world"
print(s.upper()) 输出 "HELLO WORLD"
- lower()函数:将字符串中的所有大写字母转换为小写字母。islower():判断字符串是否小写
s = "HELLO WORLD"
print(s.lower()) 输出 "hello world"
- swapcase()函数:将字符串中的大写字母转换为小写字母,将小写字母转换为大写字母。
s = "Hello World"
print(s.swapcase()) 输出 "hELLO wORLD"
- capitalize():将字符串的首字母转换为大写,其他字母转换为小写。
string = "hello world"
capitalized_string = string.capitalize()
print(capitalized_string) # 输出: Hello world
- title():将字符串中的每个单词的首字母都转换为大写。istitle():判断字符串首字母是否大写
string = "hello world"
title_string = string.title()
print(title_string) # 输出: Hello World
场景复现:注册账号时需要输入正确的验证码
account = input("请输入账户名称:")
passwd = int(input("请输入新密码:"))
passwd_confirm = int(input("再次输入新密码:"))
capcha = input("请输入验证码:")
if passwd == passwd_confirm and capcha.lower() == "ABC".lower():
print("注册成功")
内容判断函数
str.isdigit()
:判断字符串是否只包含数字。
string1 = "12345"
print(string1.isdigit()) # True
string2 = "12345a"
print(string2.isdigit()) # False
isalnum()
:判断字符串是否只包含字母和数字字符。
string1 = "Hello123"
print(string1.isalnum()) # True
string2 = "Hello123!"
print(string2.isalnum()) # False
-
isalpha()
:判断字符串是否只包含字母字符
string1 = "Hello"
print(string1.isalpha()) # True
string2 = "Hello123"
print(string2.isalpha()) # False
isspace()
:判断字符串是否只包含空白字符。
string1 = " "
print(string1.isspace()) # True
string2 = " Hello "
print(string2.isspace()) # False
endswith()
:判断字符串是否以指定字符或字符串结束。
ls = ["1.txt","2.mp3","4.mp4","3.php"]
for file in ls:
if file.endswith(".php"):
print(f"{file}是一个php文件")
startswith()
:判断字符串是否以指定字符或字符串开始。
string = "Hello, World!"
print(string.startswith("Hello")) # True
print(string.startswith("World")) # False
- join():将一个可迭代序列中的元素按照指定的分隔符连接成一个字符串。
my_list = ['apple', 'banana', 'orange']
result = ', '.join(my_list)
print(result)
输出:
apple, banana, orange
在这个示例中,我们使用join函数将列表my_list中的元素用逗号和空格连接成一个字符串,并将结果打印出来。
编码/分隔
- encode():将字符串编码为指定的字符编码格式的函数。
- ord():获取指定字符串的编码格式。
- split():用于将字符串按照指定的分隔符进行拆分。
s = "你好"
encoded = s.encode("utf-8")
print(encoded) # b'\xe4\xbd\xa0\xe5\xa5\xbd'
在上述例子中,字符串s
使用encode("utf-8")
函数将其转换为utf-8编码的字节流,结果为b'\xe4\xbd\xa0\xe5\xa5\xbd'
。
s = "Hello,world,Python"
splitted = s.split(",")
print(splitted) # ['Hello', 'world', 'Python']
在上述例子中,字符串s
使用split(",")
函数将其按照逗号进行拆分,结果为['Hello', 'World!']
。
去除空格
- strip():去除字符串两端的空格
- lstrip():去除字符串左端空格
- rstrip():去除字符串右端空格
s = " hello world "
new_s = s.strip()
print(new_s) # 输出:hello world
对齐方式
在Python中,可以使用字符串的ljust()
, rjust()
和center()
方法来实现字符串的对齐。
ljust(width, fillchar)
函数用于将字符串左对齐,并使用fillchar
字符填充不足的部分,返回一个新的字符串。其中,width
参数表示字符串最终的宽度,fillchar
参数表示填充字符,默认为空格。
string = "Hello"
new_string = string.ljust(10, '-')
print(new_string) # 输出:Hello-----
rjust(width, fillchar)
函数用于将字符串右对齐,并使用fillchar
字符填充不足的部分,返回一个新的字符串。参数和ljust()
函数相同。
string = "Hello"
new_string = string.rjust(10, '-')
print(new_string) # 输出:-----Hello
center(width, fillchar)
函数用于将字符串居中对齐,并使用fillchar
字符填充不足的部分,返回一个新的字符串。参数和ljust()
函数相同。
string = "Hello"
new_string = string.center(10, '-')
print(new_string) # 输出:--Hello---
查找目标字符串序号
在python中,index函数用于查找指定元素在列表中的索引位置。它的语法如下:
list.index(element, start, end)
- element: 要查找的元素。
- start: 开始搜索的索引位置(可选,默认为0)。
- end: 结束搜索的索引位置(可选,默认为列表长度)。
- rindex():从右开始查找 lindex():从左开始查找
s = "abc123"
print(s.index("3")) #输出c