python基础--字符串常用函数
2017-11-24 15:53
537 查看
#_*_coding:utf-8_*_ """ 字符串常用函数 """ str1 = 'hello world' """ find() 检查字符串是否包含指定的字符, 没包含返回-1, 包含返回开始的索引值 """ # 3 print(str1.find('lo')) # -1 print(str1.find('yy')) """ index() 检查字符串是否包含指定的字符, 包含-返回开始的索引值 不包含-提示错误 """ # 3 print(str1.index('lo')) # ValueError: substring not found # print(str1.index('yy')) """ count 返回str1在string中指定索引返回内[start, end]出现的次数 """ print(str1.count('lo')) print(str1.count('lo', 5, len(str1))) """ replace 将str1中的str1替换成str2,如果指定count,则不超过count次数 """ print(str1.replace('hello', 'HELLO')) print(str1.replace('hello', 'HELLO', 1)) print(str1.replace('l', 'L', 2)) """ split 如果maxsplit有指定值,则仅分割maxsplit个子字符串 """ str1 = 'hello world hello china' # ['hello', 'world', 'hello', 'china'] print(str1.split(' ')) # ['hello', 'world hello china'] print(str1.split(' ', 2)) """ capitalize 将字符串的首字母大写 """ str1 = 'hello world hello china' print(str1.capitalize()) """ title 把字符串中每个单词的首字母大写 """ print(str1.title()) """ startswith 检查字符串是否以obj开头, 是 返回 Ture 否 返回 False endswith 检查字符串是否以obj结尾 是 返回 True 否 返回 False """ str1 = 'hello world hello china' print(str1.startswith('hello')) """ lower 将字符串转换为小写 upper 将字符串转换为大写 """ str1 = 'hello world hello china' print(str1.lower()) print(str1.upper()) """ ljust 返回一个原字符串左对齐,并使用空格填充至长度width的新字符串 rjust 返回一个字符串右对齐,并使用空格填充至长度width的心字符串 center 返回一个原字符串居中,并使用空格填空至长度width的新字符串 """ str1 = 'hello' # hello , print(str1.ljust(10)) # , hello print(str1.rjust(10)) # , hello , print(str1.center(10)) """ lstrip 去除字符串左边空白字符 rstrip 去除字符串右边空白字符 strip 去除两边空白字符 """ str1 = ' hello' print(str1.lstrip()) str1 = 'hello ' print(str1.rstrip()) str1 = ' hello ' print(str1.strip()) """ partition 可以将字符串以str1进行分割成三个部分,str1前, str1, str1后 """ str1 = 'hello world hello china' # ('hello ', 'world', ' hello china') print(str1.partition('world')) """ join list中每个字符串后面插入str1,构造出一个新的字符串 """ str1 = '_' list=['hello', 'world', 'hello', 'china'] print(str1.join(list)) """ isspace 如果str1中只包含空格,则返回True,否则返回False """ str1=' ' print(str1.isspace()) """ isalnum 如果str1所有字符都是字母或数字则返回True,否则返回False isdigit 如果str1只包含数字则返回True,否则返回False isalpha 如果str1所有字符都是字母,则返回True,否则返回False """ str1='a123' print(str1, str1.isalnum()) print(str1.isdigit()) print(str1.isalpha())
相关文章推荐
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- 零基础学python-7.1 python中的字符串简介与常用函数
- Python基础、函数及其相关函数库(Numpy、TensorFlow)常用函数集锦(更新中,更新日期:2017-08-03)
- python 字符串常用函数方法整合
- python基础类型-常用函数
- 1. python 字符串简介与常用函数
- python-基础笔记6-文件处理常用函数
- Python中常用字符串 函数-转
- Python语法第4讲:字符串及常用函数
- Python常用的字符串处理函数
- python基础之字符串内建函数(部分)
- python中的字符串常用函数
- python基础:os模块中关于文件/目录常用的函数使用方法
- python 堆、队列操作、字符串格式化输出、字符串常用函数 示例代码
- 零基础学python-7.1 python中的字符串简介与常用函数
- JQuery基础(一):JQuery 常用的字符串处理函数
- Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】[原创]_python_脚本之家
- python 字符串操作常用函数总结
- python常用函数总结 分类: python基础学习 2014-02-07 14:12 260人阅读 评论(0) 收藏
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数