栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python @ 小甲鱼网课】 P8字符串(4)截取、拆分字符串

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python @ 小甲鱼网课】 P8字符串(4)截取、拆分字符串

字符串(4)截取、拆分字符串
  • 截取
    • 去除空白
    • 去除字符串中的指定字符
    • 去除字符串中的指定字符串
  • 拆分

截取 去除空白
x = "    hello world  "   
print(x.lstrip())    # 去除左侧留白
x = "  hello world     "
print(x.rstrip())    # 去除右侧留白
x = "    hello world    "
print(x.strip())    # 去除留白


去除字符串中的指定字符
x = "www.baidu.com"
print(x.lstrip("bwcom."))
x = "www.baidu.com"
print(x.rstrip(".dcom"))
x = "www.baidu.com"
print(x.strip("wcom."))


去除字符串中的指定字符串

python3.9后才有的功能

x = "www.baidu.com"
print(x.removeprefix("www."))
x = "www.baidu.com"
print(x.removesuffix(".com"))

拆分

partition()

x = "www.baidu.com"
print(x.partition("."))
print(x.rpartition("."))


split()

x = "www.baidu.com"
print(x.split("."))


splitlines()

x = "wwwn.baidun.com"
print(x.splitlines())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号