文章目录
- 前言
- 一、pandas是什么?
- 二、使用步骤
- 1.引入库
- 2.读入数据
- 总结
前言
一.字符串内置函数
大小写函数
- 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()) # Truestring2 = "12345a"
print(string2.isdigit()) # False
isalnum()
:判断字符串是否只包含字母和数字字符。
string1 = "Hello123"
print(string1.isalnum()) # Truestring2 = "Hello123!"
print(string2.isalnum()) # False
-
isalpha()
:判断字符串是否只包含字母字符
string1 = "Hello"
print(string1.isalpha()) # Truestring2 = "Hello123"
print(string2.isalpha()) # False
isspace()
:判断字符串是否只包含空白字符。
string1 = " "
print(string1.isspace()) # Truestring2 = " 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