欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Python---字符串对象和切片操作

Python---字符串对象和切片操作

2025/2/24 17:56:11 来源:https://blog.csdn.net/zhoutong2323/article/details/141636851  浏览:    关键词:Python---字符串对象和切片操作

文章目录

  • 前言
  • 一、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

总结

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词