关于python字符串方法分类详解

作者:laozhang 时间:2023-12-30 22:51:44 

python字符串方法分类,字符串是经常可以看到的一个数据储存类型,我们要进行字符的数理,就需要用各种的方法,这里有许多方法,我给大家介绍比较常见的重要的方法,比如填充、删减、变形、分切、替代和查找。

打开sublime text 3编辑器,新建一个PY文件

关于python字符串方法分类详解


test = "hey"

test_new = test.center(10, "$")

print(test_new)

填充类的有center()这个方法,可以指定字符,然后往两边填充,第一个参数是总的字符串长度。

关于python字符串方法分类详解


test = " hey   "

test_new = test.strip()

删减类的经常用来去除一些不必要的字符,比如空格,strip()可以去除两头的。

关于python字符串方法分类详解


test = "hey"

test_new = test.upper()

变形类的有upper(),也就是把英语的字符都变为大写。当然也有小写等功能。

关于python字符串方法分类详解


test = "hey you where are you"

test_new = test.split()

分切类最常见的就是split(),也就是把字符串变为列表。

关于python字符串方法分类详解


test = "hey you where are you"

test_new = test.replace("you", "U")

替代类的用replace()可以一下子替换字符内容。

关于python字符串方法分类详解

test = "hey you where are you"

test_new = test.find("where")

查找类的,可以快速定位到字符的第一个序号。

关于python字符串方法分类详解

来源:https://jingyan.baidu.com/article/925f8cb821aa9380dce05647.html

标签:python,字符串,分类
0
投稿

猜你喜欢

  • asp 防盗链代码(彻底屏蔽迅雷,旋风,快车下载站内资源)

    2011-02-26 10:46:00
  • 使用python Django做网页

    2023-11-22 03:35:26
  • 简单了解python中对象的取反运算符

    2021-04-21 16:50:57
  • python实现图片彩色转化为素描

    2021-12-02 03:57:36
  • Django nginx配置实现过程详解

    2022-11-09 11:42:58
  • Python入门:认识列表和元组

    2022-11-06 02:18:42
  • 基于Python实现自制拼图小游戏

    2021-12-01 03:25:01
  • IronPython连接MySQL的方法步骤

    2024-01-27 05:43:05
  • 微信小程序转化为uni-app项目的方法示例

    2024-03-23 19:34:39
  • Oracle 10g client 安装图解教程

    2023-07-13 20:40:10
  • 使用 Python 获取 Linux 系统信息的代码

    2022-04-15 06:32:12
  • python解析xml文件操作实例

    2022-01-02 10:39:13
  • 用django设置session过期时间的方法解析

    2022-11-16 11:36:06
  • 用VB将ASP代码封装成DLL

    2007-09-28 12:46:00
  • Magic Photo Frame 神奇创意相框

    2009-09-15 20:45:00
  • Python实现批量下载ts文件并合并为mp4

    2022-07-15 20:24:09
  • 使用python+whoosh实现全文检索

    2021-01-04 05:29:46
  • Navigator sendBeacon页面关闭也能发送请求方法示例

    2024-04-22 22:37:44
  • vue实现手机号码的校验实例代码(防抖函数的应用场景)

    2024-05-29 22:19:44
  • 让css使网页图片半透明

    2007-02-03 11:39:00
  • asp之家 网络编程 m.aspxhome.com