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

yipeiwu_com5年前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查找第k小元素代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- from random import randintfrom math import ceil, floor def...

Android应用开发中Action bar编写的入门教程

从Android 3.0开始除了我们重点讲解的Fragment外,Action Bar也是一个重要的内容,Action Bar主要是用于代替传统的标题栏,对于Android平板设备来说屏...

Python3.5.3下配置opencv3.2.0的操作方法

Python3.5.3下配置opencv3.2.0的操作方法

1.安装numpy 进入python安装目录的lib下的site-packages文件夹下打开cmd输入pip install numpy下载numpy NumPy系统是Python的...

DataFrame:通过SparkSql将scala类转为DataFrame的方法

如下所示: import java.text.DecimalFormat import com.alibaba.fastjson.JSON import com.donews.dat...

Python中在脚本中引用其他文件函数的实现方法

在导入文件的时候,Python只搜索当前脚本所在的目录,加载(entry-point)入口脚本运行目录和sys.path中包含的路径例如包的安装地址。所以如果要在当前脚本引用其他文件,除...