1、字符串的定义
a = "hello word"
2、字符串常用函数
len()
统计字符串长度
示例代码如下:
a = "hello world"
print(a) # 11
count()
统计某个字符出现的次数
示例代码如下:
a = "hello world"
print(a.count("l")) # 3
isupper()
判断字符串是否全是大写字母
示例代码如下:
a = "hello world"
print(a.isupper()) # False
islower()
判断字符串是否全是小写字母
示例代码如下:
a = "hello world"
print(a.islower()) # True
max()
返回字符串中的最大值
示例代码如下:
a = "hello world"
print(max(a)) # "w"
min()
返回字符串中的最小值
示例代码如下:
a = "hello world"
print(min(a)) # " "
isdigit()
判断字符串是否都是由数字组成
示例代码如下:
a = "hello world"
print(a.isdigit()) # False
upper()
字符串转大写
示例代码如下:
a = "hello world"
print(a.upper()) # HELLO WORLD
lower()
字符串转小写
示例代码如下:
a = "HELLO WORLD"
print(a.lower()) # hello world
swapcase()
字符串大写转小写,小写转大写
示例代码如下:
a = "ABCabc"
print(a.swapcase()) # 'abcABC'
示例代码如下:
a = "HELLO WORLD"
print(a.lower()) # hello world
strip()
去除字符串左右两边的空格
示例代码如下:
a = " hello world "
print(a.strip()) # hello world
lstrip()
去除字符串左边的空格
示例代码如下:
a = " hello world "
print(a.lstrip()) # "hello world "
rstrip()
去除字符串右边的空格
示例代码如下:
a = " hello world "
print(a.rstrip()) # " hello world"
replace()
字符串替换
返回一个新的字符串
示例代码如下:
a = " hello world "
print(a.replace("world","girl")) # "hello girl"
split()
字符串分割
返回一个列表
示例代码如下:
a = " hello world "
print(a.split(" ")) # ['hello', 'world']