capitalize
()
将字符串的第一个字符转换为大写
center
(width, fillchar)
返回一个指定的宽度
width
居中的字符串,
fillchar
为填充的字符,
默认为空格。
count
(str, beg= 0,end=len(string))
返回
str
在
string
里面出现的次数,如果
beg
或者
end
指定则返回指定范围内
str
出现的次数
endswith
(suffix, beg=0, end=len(string))
检查字符串是否以
suffix
结束,如果
beg
或者
end
指定则检查指定的范围内是否以
suffix
结束,如果是,返回
True,
否则返回
False
。
expandtabs
(tabsize=8)
把字符串
string
中的
\t
符号转为空格,
tab
符号默认的空格数是
8
find
(str, beg=0, end=len(string))
检测
str
是否包含在字符串中,如果指定范围
beg
和
end
,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回
-1
index
(str, beg=0, end=len(string))
跟
find()
方法一样,只不过如果
str
不在字符串中会报一
个异常。
isalnum
()
非空字符串 中没有符号 就返回
True
isalpha
()
判断字符串是否是英文字符 是就返回
True
isdigit
()
如果字符串只包含数字则返回
True
否则返回
False
islower
()
用于检测字符串中的所有字符是否都是小写字母
,
字符都是小写,则返回
True
,
否则返回
False
isnumeric
()
如果字符串中只包含数字字符,则返回
True
,否则返回
False
isspace
()
如果字符串中只包含空白,则返回
True
,否则返回
False.
istitle
()
如果字符串是标题化的
(
见
title())
则返回
True
,否则返回
False
isupper
()
用于检测字符串中的所有字符是否都是大写字母
,
并且都是大写,则返回
True
,否
则返回
False
join
(seq)
以指定字符串作为分隔符,将
seq
中所有的元素
(
的字符串表示
)
合并为一个新的字
符串
len
(string)
返回字符串长度
ljust
(width, fillchar])
返回一个原字符串左对齐
,
并使用
fillchar
填充至长度
width
的新字符
串,
fillchar
默认为空格。
lower
()
转换字符串中所有大写字符为小写
.
lstrip
()
截掉字符串左边的空格
,\t,\r,\n
或指定字符。
maketrans
()
创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数
是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
max
(str)
返回字符串
str
中最大的字母。
min
(str)
返回字符串
str
中最小的字母。
replace
(old, new , max)
把 将字符串中的
old
替换成
new,
如果
max
指定,则替换不超过
max
次。
rfind
(str, beg=0,end=len(string))
类似于
find()
函数,不过是从右边开始查找
.
rindex
( str, beg=0, end=len(string))
类似于
index()
,不过是从右边开始
.
rjust
(width, fillchar)
返回一个原字符串右对齐
,
并使用
fillchar(
默认空格)填充至长度
width
的新字符串
rstrip
()
删除字符串末尾的空格
\t,\r,\n
或指定字符。
split
(sep="", maxsplit=string.count(str))
以
sep
为分隔符截取字符串,如果
maxsplit
有指
定值,则仅截取
maxsplit+1
个子字符串
splitlines
(keepends)
按照行
('\r', '\r\n', \n')
分隔,返回一个包含各行作为元素的列表,如果
参数
keepends
为
False
,不包含换行符,如果为
True
,则保留换行符。
startswith
(substr, beg=0,end=len(string))
检查字符串是否是以指定子字符串
substr
开
头,是则返回
True
,否则返回
False
。如果
beg
和
end
指定值,则在指定范围内检查。
strip
(chars)
在字符串上执行
lstrip()
和
rstrip()
swapcase
()
将字符串中大写转换为小写,小写转换为大写
title
()
返回
"
标题化
"
的字符串
,
就是说所有单词都是以大写开始,其余字母均为小写
序
号
upper
()
转换字符串中的小写字母为大写
zfill
(width)
在字符串左侧填充指定数量的零,确保整个字符串达到指定长度
1. str.lower(): 将字符串转换为小写
text = "Hello, World!"
print(text.lower()) # 输出: hello, world!
2. str.upper(): 将字符串转换为大写
text = "Hello, World!"
print(text.upper()) # 输出: HELLO, WORLD!
3. str.strip(): 移除字符串两端的空白字符
text = " Hello, World! "
print(text.strip()) # 输出: Hello, World!
4. str.split(): 将字符串拆分为列表
text = "Hello, World!"
print(text.split()) # 输出: ['Hello,', 'World!']
text = "Hello,World!"
print(text.split(',')) # 输出: ['Hello', 'World!']
5. str.join(): 将列表中的字符串连接为一个字符串
words = ['Hello', 'World']
print(" ".join(words)) # 输出: Hello World
words = ['apple', 'banana', 'cherry']
print(", ".join(words)) # 输出: apple, banana, cherry
6. str.replace(): 替换字符串中的子字符串
text = "Hello, World!"
print(text.replace("World", "Python")) # 输出: Hello, Python!
7. str.find(): 查找子字符串,返回第一次出现的位置
text = "Hello, World!"
print(text.find("World")) # 输出: 7
print(text.find("Python")) # 输出: -1(如果未找到则返回 -1)
8. str.startswith(): 检查字符串是否以指定子字符串开头
text = "Hello, World!"
print(text.startswith("Hello")) # 输出: True
print(text.startswith("World")) # 输出: False
9. str.endswith(): 检查字符串是否以指定子字符串结尾
text = "Hello, World!"
print(text.endswith("World!")) # 输出: True
print(text.endswith("Hello")) # 输出: False
10. str.isdigit(): 检查字符串是否只包含数字字符
text1 = "12345"
text2 = "12345abc"
print(text1.isdigit()) # 输出: True
print(text2.isdigit()) # 输出: False
11. str.isalpha(): 检查字符串是否只包含字母字符
text1 = "Hello"
text2 = "Hello123"
print(text1.isalpha()) # 输出: True
print(text2.isalpha()) # 输出: False
12. str.isalnum(): 检查字符串是否只包含字母和数字字符
text1 = "Hello123"
text2 = "Hello 123"
print(text1.isalnum()) # 输出: True
print(text2.isalnum()) # 输出: False
13. str.title(): 将字符串中的每个单词的首字母转换为大写
text = "hello, world!"
print(text.title()) # 输出: Hello, World!
14. str.capitalize(): 将字符串的首字母转换为大写
text = "hello, world!"
print(text.capitalize()) # 输出: Hello, world!
15. str.count(): 计算子字符串在字符串中出现的次数
text = "Hello, World! Hello, Python!"
print(text.count("Hello")) # 输出: 2
print(text.count("Python")) # 输出: 1
16. str.format(): 格式化字符串
name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
# 输出: My name is John and I am 30 years old.
print(f"My name is {name} and I am {age} years old.")
# 输出: My name is John and I am 30 years old. (使用 f-string)