参考链接: Python字符串| ljust rjust center

python为字符串操作提供了很多常用的内建函数,让我们在操作字符串时能够更加简单方便。

下面以某个字符串为例来一一介绍一下

现有字符串:mystr = ‘hello world and hello python’

find(self, sub, start=None,end=None)

find 函数用于在给定的字符串中查找某个子字符串是否存在,如果找到则返回该子串的第一次出现的索引位置,否则返回-1

其中,self参数不用传递,sub就是要找的子字符串,start和end分别是字符串的开始和结束位置,如果不传递则默认从字符串的开始到字符串的结束位置。

使用方式:字符串.find(),比如查找python出现的位置

mystr = 'hello world and hello python'

#1.在整个字符串中查找

print(mystr.find('python'))

#输出结果

22

#2.在字符串的指定位置内查找

print(mystr.find('python',0,20))

#返回结果

-1

rfind(self, sub, start=None,end=None)

与find用法类似,只不过是从字符串的右侧开始查找,但索引依然是从左边计算

mystr = 'hello world and hello python'

#1.在整个字符串中查找

print(mystr.rfind('python'))

#输出结果

22

index(self,sub,start=None,end=None)

index函数与find函数的参数和用法一样,也是直接返回子串的索引位置。唯一不同的是当没有找到子串时不会返回-1而是直接报异常。示例代码:

mystr = 'hello world and hello python'

#1.在整个字符串中查找

print(mystr.index('python'))

#输出结果

22

#2.在字符串的指定位置内查找

print(mystr.index('python',0,20))

#返回结果

报异常:ValueError:substring not found

rindex(self,sub,start=None,end=None)

与index函数用法和功能一下,不同的是也是从字符串的右侧开始查找,但索引依然是从左侧计算

mystr = 'hello world and hello python'

#1.在整个字符串中查找

print(mystr.rindex('python'))

#输出结果

22

count(self,sub,start=None,end=None)

count函数用于统计sub子串在字符串指定位置中出现的次数,start和end不传递则默认匹配整个字符串

#1.在整个字符串中统计

print(mystr.count('python'))

#输出结果

1

#2.在字符串的指定位置内统计

print(mystr.count('python',0,20))

#返回结果

0

replace(self,oldstr,newstr,count)

replace用法还是与其它函数用法相同,但参数有所不同。oldstr将被替换的字符串,newstr替换后新的字符串,count为int类型如果不传递则默认替换所有的oldstr,如果count指定了值则替换不超过count次。返回值为替换后的新字符串

#1.替换所有匹配到的字符串

print(mystr.replace('hello','hi'))

#输出结果

hi world and hi python

#2.只替换一次

print(mystr.replace('hello','hi',1))

#返回结果

hi world and hello python

split(sub,maxsplit)

split函数用法不变,主要用于以sub为分隔符对整个字符串切片,可以不传递任何参数,则默认会根据空格进行切片,如果maxsplit有指定值,则仅分割maxsplit个子字符串。其返回结果为列表类型

#1.替换所有匹配到的字符串

print(mystr.split())

print(mystr.split(' '))

#两条语句返回结果相同

['hello','world','and','hello','python']

['hello','world','and','hello','python']

#2.只分割2个

print(mystr.split(' ',2))

#返回结果

['hello','world','and hello python']

capitalize

capitalize用于将字符串的第一个字符转换为大写,返回值为转换后的新字符串

print(mystr.capitalize())

#输出结果

Hello world and hello python

title

title函数与cacapicapitalize函数类似也是把字符串转换为大写,但不同的是title函数会将字符串中的每个单词的首字母都转换为大写,返回值为转换后的新字符串

print(mystr.title())

#输出结果

Hello World And Hello Python

startswith

startswith函数用于判断一个字符串是否是以某个子字符串开头,如果是返回True否则返回False

#1. 判断是否以hello开头

print(mystr.startswith('hello'))

#输出结果

True

#2. 判断是否以python开头

print(mystr.startswith('python'))

#输出结果

Flase

endwith

与startswith相同用于判断是否以某个字符串结尾,是返回True,否则返回False

#1. 判断是否以python结尾

print(mystr.endwith('python'))

#输出结果

True

#2. 判断是否以hello结尾

print(mystr.endwith('hello'))

#输出结果

Flase

lower

lower函数用于将字符串中所有大写字符转换为小写

mystr = 'HELLO WORLD and Hello Python'

print(mystr.lower())

#输出结果

hello world and hello python

upper

upper函数与lower函数相反,是将字符串中所有小写字母全部转换为大写

mystr = 'hello world and Hello Python'

print(mystr.upper())

#输出结果

HELLO WORLD AND HELLO PYTHON

ljust(width) rjust(width) center(width)

这是3个调整字符串对齐的函数,参数width表示字符串的长度,如果原始字符串长度不够则用空格来填充,最终返回一个新的字符串

mystr = 'hello'

print(mystr.ljust(10))

print(mystr.rjust(10))

print(mystr.center(10))

#输出结果

'hello     '

'     hello'

'  hello   '

lstrip()

用于删除字符串左侧的所有空格,返回操作后的新字符串

mystr = '      hello python'

print(mystr.lstrip())

#输出结果

hello python

rstrip()

用于删除字符串右侧的所有空格,返回操作后的新字符串

mystr = 'hello python          '

print(mystr.rstrip())

#输出结果

hello python

strip()

删除字符串左右两侧的所有空格

mystr = '      hello python          '

print(mystr.strip())

#输出结果

hello python

partition()

该函数用于将一个字符串以某个子串分割为3个部分,分别是子串前,子串和子串后,返回一个分割后的列表

mystr = 'hello wrold and hello python'

mystr.partition('and')

#输出结果

['hello world','and','hello python']

rpartition()

与partition一样也是将一个字符串以某个子串分割为3个部分,分别是子串前,子串和子串后,返回一个分割后的列表。不同的是该函数将从字符串的右侧开始分割

mystr = 'hello python and python hello'

mystr.rpartition('python')

#输出结果

['hello python and','python','hello']

splitlines()

splitlines函数用于以行为单位对字符串进行分割,结果返回分割后的列表n

等同于split('\n')

mystr = 'hello\nworld\nand\nhello\npython'

print(mystr.splitlines())

print(mystr.split('\n'))

#输出结果

['hello','world','and','hello','python']

['hello','world','and','hello','python']

isalpha()

isalpha用于判断一个字符串中,是否所有的字符都是字母,如果是返回True否则返回False

mystr = 'hello world'

print(mystr.isalpha())

#输出结果

True

mystr = 'hello 123'

print(mystr.isalpha())

#输出结果

False

mystr = '123'

print(mystr.isalpha())

#输出结果

False

isdigit()

isdigit用于判断一个字符串中,是否所有的字符都是数字,如果是返回True否则返回False

mystr = 'hello world'

print(mystr.isdigit())

#输出结果

False

mystr = 'hello 123'

print(mystr.isdigit())

#输出结果

False

mystr = '123'

print(mystr.isdigit())

#输出结果

True

isalnum()

isalnum用于判断一个字符串中,是否所有的字符只包含字母或者数字,如果是返回True否则返回False

mystr = 'helloworld'

print(mystr.isalnum())

#输出结果

True

mystr = 'hello world'

print(mystr.isalnum())

#输出结果

False

mystr = 'hello 123'

print(mystr.isalnum())

#输出结果

False

mystr = 'hello123'

print(mystr.isalnum())

#输出结果

True

mystr = '123'

print(mystr.isalnum())

#输出结果

True

isspace()

isspace用于判断一个字符串中,是否只包含空格,如果是返回True否则返回False

mystr = 'helloworld'

print(mystr.isspace())

#输出结果

False

mystr = 'hello world'

print(mystr.isspace())

#输出结果

False

mystr = ''

print(mystr.isspace())

#输出结果

False

mystr = ' '

print(mystr.isspace())

#输出结果

True

mystr = '      '

print(mystr.isspace())

#输出结果

True

join()

join的参数一般是一个可迭代对象,如列表、元组或集合,用于将可迭代对象中的元素以某个字符串进行连接,组合成一个新的字符串,

参数也可以是一个字符串,那么组合后的字符串就是在每个字符后面插入某个连接字符

#1.参数是一个列表

lst = ['hello','world','and','hello','python']

#以空格的形式进行连接

print(' '.join(lst))

#以下划线的形式进行连接

print('_'.join(lst))

#输出结果

hello_world_and_hello_python

#2. 参数是一个字符串

mystr = 'hello world and hello python'

print('_'.join(mystr))

#输出结果

h_e_l_l_o_ _w_o_r_l_d_ _a_n_d_ _h_e_l_l_o_ _p_y_t_h_o_n

[转载] python 语言基础 - 字符串常用函数及操作相关推荐

  1. 【python语法】字符串常用函数

    基本函数 dir() dir()函数的作用是将参数数据类型包含的所有函数返回到一个列表中,这个列表是可遍历的:可以直接输出,也可以循环输出参数类型的所有内嵌函数 直接输出字符串数据类型带有的所有函数: ...

  2. C语言中字符串常用函数--strcat,strcpy

    strcpy 原型声明:extern char *strcpy(char* dest, const char *src); 头文件:#include <string.h> 功能:把从src ...

  3. python中的字符串常用函数

    str.isalnum():判断字符串是否由字母.数字组成,返回True或者False str.isalpha():判断字符串是否由字母组成 str.isdigit():判断字符串是否由数字组成 st ...

  4. c语言字符串未初始化strcat,C语言中字符串常用函数strcat与strcpy的用法介绍

    strcpy原型声明:extern char *strcpy(char* dest, const char *src); 头文件:#include 功能:把从src地址开始且含有NULL结束符的字符串 ...

  5. C语言的字符串常用函数(18种)

    题目 1.gets() 2. fgets() 3. puts() 4. fputs() 5. sprintf() 6. fprintf() 7. sscanf() 8. atoi(), atol(), ...

  6. python语言基础实验_实验二Python语言基础函数包练习.doc

    实验二Python语言基础函数包练习 实验 Python语言基础函数包练习:1208 学号: 实验目的 1.Python语言包,如math.NumPySciPy和Matplotlib等函数包的使用实验 ...

  7. 【python语言基础】疑难点整理2

    [python语言基础]疑难点整理1 第五章 在python语法中,循环体中的语句没有做限制,因此,可以是任何合法语句,当然也可以是循环语句.这样就形成了循环语句的嵌套. while循环语句和for循 ...

  8. python全套完整教程-Python语言基础50课 全套完整版(含doc版)

    Python 语言基础 50 课是一套新的简单的给新手小白Python入门教程,由于之前发布的Python学习项目Python-100-Days对初学者来说上手还是有一定难度,而且很多小伙伴希望能够有 ...

  9. python课程教学大纲-《Python语言基础》课程教学大纲

    <Python语言基础>课程教学大纲 课程名称:Python语言基础课程类别:专业选修课 适用专业:电子信息工程考核方式:考查 总学时.学分:32学时1.5学分 其中讲授16学时,实验10 ...

最新文章

  1. WCF 入门之旅(4): 怎样用客户端调用WCF服务
  2. python表白程序-python如何写出表白程序
  3. 机器学习笔记(四)决策树
  4. 备战实习求职的一些感想(已拿阿里offer)
  5. xcode 可以打开xmind_原来xmind还有一款如此漂亮的思维导图工具
  6. 具有InlfuxDB的Spring Boot和Micrometer第1部分:基础项目
  7. java基础,继承类题目:编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E...
  8. 随想录(学习英文的好处)
  9. HZOJ visit
  10. oracle wmsys.wm_concat函数
  11. 这款微信插件太好用了
  12. python查火车票_Python查询火车票(三)
  13. pandas——显示设置
  14. 计算机钢琴汇编设计报告,汇编梦幻钢琴程序设计报告.doc
  15. 第三方可视化数据分析图表Pyecharts(下载保存图片(生成的html图片)、zip函数(将数据转换为列表加元组的格式)、南丁格尔玫瑰图、双y轴可视化、饼形图和环形图)
  16. 【opencv】19.图像边缘检测算子数学原理、像素一二阶导数的意义
  17. 文件上传控件-如何上传文件-文件夹上传
  18. Magic Leap开发指南(7)-- 眼球追踪(Unity)
  19. 黑五节日营销,Facebook广告投放指南
  20. 惊天大突破!「我国数学家证明 NP=P」!道翰天琼认知智能机器人平台API接口大脑为您揭秘。

热门文章

  1. 【CCCC】L3-023 计算图 (30分),dfs搜索+偏导数计算
  2. 设计模式—适配器模式(思维导图)
  3. 寻找无向图的关节点(Articulation Points)和判断图是否是双连通图(Biconnect Graph)
  4. Qt UDP组播的应用
  5. 在一个请求分页系统中,分别采用 FIFO、LRU和 OPT页面置换算法时,假如一个作业的页面走向为 4、3、2、1、4、3、5、4、3、2、1、5,当分配给该作业的物理块数M分别为 3、4时,
  6. Unity3D基础27:C#随机函数与物体销毁
  7. 牛客国庆集训派对Day2: E. 数据排序(状压DP+记忆化搜索)
  8. 2018 Multi-University Training Contest 4: B. Harvest of Apples(分块打表)
  9. HDU 2063:过山车(匈牙利算法模板题)
  10. matlab实现直方图均衡化