当前位置:首页>python>Python中组合数据类型常用“字符串提取和分割与合并”

Python中组合数据类型常用“字符串提取和分割与合并”

  • 2026-09-09 04:27:48
Python中组合数据类型常用“字符串提取和分割与合并”

Python中组合数据类型常用“字符串提取和分割与合并”

strip()去除字符串两边的指定字符(默认去除的是空格)

案例学习:
str1="today is a nice day"
str2="***today is a nice day***"
# print(str1)
print(str1.strip())
# print(str2)
# print(str2.strip("*"))
# lstrip() 只去除字符串左边的指定字符
# print(str2.lstrip("*"))
# rstrip() 只去除字符串右边的指定中字符
# print(str2.rstrip("*"))
# 字符串的分割和合并
# split() 以指定字符对字符串进行分割(默认是空格)

str3="this is a string example...."
print(str3.split()) ['this','is','a','string','example....']
# print(str3.split("i"))
#['th','s a str','ng example....']
# join() 合并字符串
str4 = "-"
tup = ("hello","every","body")
print(tup)
print(str4.join(tup))

输出结果如下:
today is a nice day
today is a nice day***
today is a nice day***
today is a nice day******
today is a nice day['this','is','a','string','example....']
['th','s','s a str','ng example....']
('hello','every','body')

#python入门#Python编程技巧#Python组合数据类型#数据处理实战#字符串提取与分割#Python学习笔记

名称已清空
微信扫一扫赞赏作者
喜欢作者其它金额
作品
暂无作品
喜欢作者
其它金额
最低赞赏 ¥0
其它金额
赞赏金额
¥
最低赞赏 ¥0
1
2
3
4
5
6
7
8
9
0
.
北京,16分钟前,

最新文章

随机文章