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

yipeiwu_com6年前Python基础

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

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

test = "hey"

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

print(test_new)

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

test = " hey   "

test_new = test.strip()

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

test = "hey"

test_new = test.upper()

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

test = "hey you where are you"

test_new = test.split()

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

test = "hey you where are you"

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

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

test = "hey you where are you"

test_new = test.find("where")

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

以上就是本次介绍的关于python字符串方法分类的全部知识点内容,感谢大家的阅读和对【听图阁-专注于Python设计】的支持。

相关文章

Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析

本文实例讲述了Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)。分享给大家供大家参考,具体如下: demo.py(类方法,@classm...

Python从list类型、range()序列简单认识类(class)【可迭代】

本文实例讲述了Python从list类型、range()序列简单认识类(class)。分享给大家供大家参考,具体如下: list类型 定义: items = [] 这就定义了一...

python2 与 python3 实现共存的方法

python2 与 python3 实现共存的方法

1.现在我本机系统已内置python2.6 2.下载进行源码安装 复制链接下载到/root/mypackage,解压 接着 mkdir /usr/local/python3 然后在解...

Python对象体系深入分析

Python对象体系深入分析

本文较为详细的分析了了Python的对象体系。分享给大家供大家参考。具体如下: Guido用C语言创造了Python,在Python的世界中一切皆为对象. 一.C视角中的Python对象...

TensorFlow的权值更新方法

一. MovingAverage权值滑动平均更新 1.1 示例代码: def create_target_q_network(self,state_dim,action_dim,ne...