Python中字符串方法有哪些具体应用场景?
- 内容介绍
- 文章标签
- 相关推荐
本文共计645个文字,预计阅读时间需要3分钟。
本篇教程参考自《Python基础教程(第二版)》,主要介绍操作、语法、实例及结果。
1. 查找子串位置:使用`find(str)`方法,返回子串所在位置的最左侧索引。
2.未找到子串:返回-1。
3.可接受起始点和结束点参数:`find(str, start, end)`,范围在左闭右开。
本文参考自《python基础教程 (第二版)》
操作
语法
举例
结果
查找子串
find("str")
1.返回子串所在位置的最左端索引
2.没有找到返回-1
3.可以接受起始点和结束点参数,范围左闭右开
title = "Monty Python's Flying Circus"
1.title.find("Monty")
title.find("Python")
2.title.find("Zircus")
3.title.find(“Circus”, 0, 22) #提供起始点和结束点
0
6
-1
-1
添加序列
"sep".join(seq)
1.split的逆方法
1.sep = "+"
seq = ["1", "2", "3", "4", "5"]
sep.join(seq)
2.sep = "/"
dirs= ["", "usr", "bin", "env"]
" sep.join(dirs)
"1+2+3+4+5"
"/usr/bin/env"
分割序列
str.split("sep")
1.join的逆方法
1."1+2+3+4+5".split("+")
2."/usr/bin/env".split("/")
["1", "2", "3", "4", "5"]
["", "usr", "bin", "env"]
小写字母
str.lower()
1."Trondheim Hammer Dance".lower()
"trondheim hammer dance"
大写字母
str.upper()
1."Trondheim Hammer Dance".upper()
"TRONDHEIM HAMMER DANCE"
大写小写互换
str.swapcase()
1. "aAsmr3idd4bgs7Dlsf9eAF".swapcase()
"AaSMR3IDD4BGS7dLSF9Eaf"
标题转换
string.capwords()
1.import string
string.capwords("that's all, folks")
"That's All, Folks"
替换
str.replace(old,new)
1."This is a test".replace("is", "ezz")
'Thezz ezz a test'
去除空格
str.strip()
1.去除两侧空格(不含内部)
2.指定需要去除的字符,将它们作为参数
1." internall whitespace is kept ".strip()
2."*** SPAM * for * everyone!!! ***".strip(" *!")
"internall whitespace is kept"
"SPAM * for * everyone"
str.lstrip()
去除左侧空格
1." internall whitespace is kept ".lstrip()
"internall whitespace is kept "
str.rstrip()
去除右侧空格
1." internall whitespace is kept ".rstrip()
" internall whitespace is kept"
本文共计645个文字,预计阅读时间需要3分钟。
本篇教程参考自《Python基础教程(第二版)》,主要介绍操作、语法、实例及结果。
1. 查找子串位置:使用`find(str)`方法,返回子串所在位置的最左侧索引。
2.未找到子串:返回-1。
3.可接受起始点和结束点参数:`find(str, start, end)`,范围在左闭右开。
本文参考自《python基础教程 (第二版)》
操作
语法
举例
结果
查找子串
find("str")
1.返回子串所在位置的最左端索引
2.没有找到返回-1
3.可以接受起始点和结束点参数,范围左闭右开
title = "Monty Python's Flying Circus"
1.title.find("Monty")
title.find("Python")
2.title.find("Zircus")
3.title.find(“Circus”, 0, 22) #提供起始点和结束点
0
6
-1
-1
添加序列
"sep".join(seq)
1.split的逆方法
1.sep = "+"
seq = ["1", "2", "3", "4", "5"]
sep.join(seq)
2.sep = "/"
dirs= ["", "usr", "bin", "env"]
" sep.join(dirs)
"1+2+3+4+5"
"/usr/bin/env"
分割序列
str.split("sep")
1.join的逆方法
1."1+2+3+4+5".split("+")
2."/usr/bin/env".split("/")
["1", "2", "3", "4", "5"]
["", "usr", "bin", "env"]
小写字母
str.lower()
1."Trondheim Hammer Dance".lower()
"trondheim hammer dance"
大写字母
str.upper()
1."Trondheim Hammer Dance".upper()
"TRONDHEIM HAMMER DANCE"
大写小写互换
str.swapcase()
1. "aAsmr3idd4bgs7Dlsf9eAF".swapcase()
"AaSMR3IDD4BGS7dLSF9Eaf"
标题转换
string.capwords()
1.import string
string.capwords("that's all, folks")
"That's All, Folks"
替换
str.replace(old,new)
1."This is a test".replace("is", "ezz")
'Thezz ezz a test'
去除空格
str.strip()
1.去除两侧空格(不含内部)
2.指定需要去除的字符,将它们作为参数
1." internall whitespace is kept ".strip()
2."*** SPAM * for * everyone!!! ***".strip(" *!")
"internall whitespace is kept"
"SPAM * for * everyone"
str.lstrip()
去除左侧空格
1." internall whitespace is kept ".lstrip()
"internall whitespace is kept "
str.rstrip()
去除右侧空格
1." internall whitespace is kept ".rstrip()
" internall whitespace is kept"

