python字符串基础函数

1、format格式化函数

用法:

# 方法一:
score_math = 95
score_Chinese = 90
score_English =100
s = "小明的数学成绩是{},英语成绩是{},语文的成绩是{}。我的数学和小明的成绩一样是{}".format(score_math,score_English,score_Chinese,score_math)
print(s)
# 运行结果 :小明的数学成绩是95,英语成绩是100,语文的成绩是90。我的数学和小明的成绩一样是95
# 方法二
s = "小明的数学成绩是{0},英语成绩是{1},语文的成绩是{2}。我的数学和小明的成绩一样是{0}".format(score_math,score_English,score_Chinese)
print(s)
# 运行结果:小明的数学成绩是95,英语成绩是100,语文的成绩是90。我的数学和小明的成绩一样是95
# 方法三
s = "小明的数学成绩是{score_math},英语成绩是{score_English},语文的成绩是{score_Chinese}。我的数学和小明的成绩一样是{score_math}".format(score_math = '95',score_Chinese = '90',score_English ='100')
print(s)
# 运行结果:小明的数学成绩是95,英语成绩是100,语文的成绩是90。我的数学和小明的成绩一样是95
2.random函数

产生随机数

# 使用该函数应当先用import调用random模块
import random
num = random.randint(1,100)
print(num)
3、find函数

从左往右找,找到第一个,便结束,没有找到任何一个符合要求的数便返回 -1(查找一个单词以第一个单词为准)

# fand函数是找到字符所在的位置索引
s = 'https://www.runoob.com/python/att-string-format.html'
n = int(s.find("."))
print(n)
#运行结果:11
4、rfind函数

从右往左寻找,找到第一个,便结束

#rfind函数与find函数用法一样只是方向相反
s = 'https://www.runoob.com/python/att-string-format.html'
n = int(s.rfind("."))
print(n)
# 运行结果:47
5、count函数

查找出现的次数(统计指定字符个数)

# 使用count函数查找(.)的个数
s = 'https://www.runoob.com/python/att-string-format.html'
time=s.count(".")
print(time)
# 运行结果:3
6、index函数

与find函数的方法类似,从左往右查找,找到第一个便结束。但没有找到会报错。

# index函数查找(.)的位置
s = 'https://www.runoob.com/python/att-string-format.html'
n = s.index('.')
print(n)
# 运行结果:11
7、rindex函数

与rfind函数类似从右往左查找

# rindex函数查找(.)的位置
s = 'https://www.runoob.com/python/att-string-format.html'
n = s.rindex('.')
print(n)
#运行结果:47
8、startswith 函数

判断是否以xxx开头,输出结果为布尔型

# startswith 函数判断是否以https开头
s = 'https://www.runoob.com/python/att-string-format.html'
print(s.startswith("https"))
#输出结果:Ture
9、endswith函数

判断是否以xxx结尾,输出结果为布尔型

#endswith 函数判断是否以html结尾
s = 'https://www.runoob.com/python/att-string-format.html'
print(s.endswith("html"))
# 输出结果:Ture
10、len函数

len函数统计字符串字数的

# 使用len函数统计下面字符串的长度
s = 'https://www.runoob.com/python/att-string-format.html'
print(len(s))
# 运行结果:52
11、replace函数

替换文本内容,先输入旧的字符,在输入新的字符,还可以规定替换次数

# 使用replace 函数替换五次“你好”为“真美
s ="春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!"
print(s.replace('你好','真美',5))
# 春天真美!春天真美!春天真美!春天真美!春天真美!春天你好!春天你好!
12、split函数

以切割符切割字符串,并且把切割的字符串封装成列表

# 使用split函数以(你)为分割符分割
s ="春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!"
print(s.split("!"))
#运行结果:['春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!']
13、rsplit函数

与split函数切割的方向相反,rsplit函数是从右往左切割

# 使用rsplit函数以(你)为分割符分割
s ="春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!春天你好!"
print(s.rsplit("你"))
# 运行结果:['春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!春天', '好!']
14、splitlines函数

按行切割

使用splitlines函数切割一首诗
shi = '''静夜思
床前看月光,疑是地上霜。
抬头望山月,低头思故乡。'''
print(shi.splitlines())
#运行结果:['静夜思', '床前看月光,疑是地上霜。', '抬头望山月,低头思故乡。']
15、title、upper、lower、capitalize 函数

title:每个单词的首字母大写

upper:转为大写

lower:转为小写

capitalize:每一行的第一个字大写

# title、upper、lower、capitalize 函数的运用
shi = '''
hello world !
hello world !
hello world !
hello world !
hello world !
'''
print(shi.title())
print(shi.upper())
print(shi.lower())
print(shi.capitalize())
#运行结果:Hello World !
Hello World !
Hello World !
Hello World !
Hello World !


HELLO WORLD !
HELLO WORLD !
HELLO WORLD !
HELLO WORLD !
HELLO WORLD !


hello world !
hello world !
hello world !
hello world !
hello world !


hello world !
hello world !
hello world !
hello world !
hello world !
16、strip、lstrip、rstrip函数

strip:去掉开头和结尾的空格

lstrip:去掉开头的空格

rstrip:去掉结尾的空格

# strip、lstrip、rstrip函数的使用
s = "       hello world!     "
print(len(s))
print(s.strip())
print(len(s.strip()))
print(s.lstrip())
print(len(s.lstrip()))
print(s.rstrip())
print(len(s.rstrip()))
# 运行结果:24
hello world!
12
hello world!     
17
       hello world!
19
17、center、ljust、rjust函数

center:利用空格居中

ljust:左对齐

rjust:右对齐

# 利用center函数是“hello world!”居中,用ljust使之左对齐,用rjust使之有对齐:
s = 'hello world!'
print(s.center(50))
print(s.ljust(50))
print(s.rjust(50))
# 运行结果:
                   hello world!                   
hello world!                                      
                                      hello world!