python字符串常用内建函数总结
自己总结一些常用字符串函数,理解比较粗糙
1.字符串内建函数-大小写转换函数
(1)str.capitalize
Help on method_descriptor:
capitalize(...)
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
返回值首字母大写,其余小写,不改变数据本身
实例:
a = “start”
a.caplitalize()
Start
(2)str.title()
Help on method_descriptor:
title(...)
S.title() -> str
Return a titlecased version of S, i.e. words start with title case
characters, all remaining cased characters have lower case
返回值首字母大写,其余都小写
实例:
a = “start”
a.title()
Start
a = “sTART”
a.title()
Start
(3)str.lower()
Help on method_descriptor:
lower(...)
S.lower() -> str
Return a copy of the string S converted to lowercase.
返回值字母大写转换为小写,大转小
实例:
a = “START”
a.lower()
start
(4)str.upper()
Help on method_descriptor:
upper(...)
S.upper() -> str
Return a copy of S converted to uppercase.
返回值字母小写转换为大写,小转大
实例:
a = “start”
a.upper()
START
(5)str.swapcase()
Help on method_descriptor:
swapcase(...)
S.swapcase() -> str
Return a copy of S with uppercase characters converted to lowercase
and vice versa.
返回值字母大写转换成小写,小写转换成大写
实例:
a = “Start”
a.swapcase()
sTART
2.字符串内建函数-搜索函数
(1)str.find()
Help on method_descriptor:
find(...)
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
S.find(sub[, start[, end]])
搜索字符串在指定的索引范围是否包含子字符串的索引值,否则返回-1
参数:
sub –指定索引的字符串
start -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度
实例:
a = “start”
b = “hd”
c = “ar”
a.find(b)
-1
a.find(c)
2
(2)str.index()
Help on method_descriptor:
index(...)
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
搜索字符串在指定的索引范围是否包含子字符串的索引值,与find方法一样,只不过如果str不在字符串中回报一个异常
实例:
a = “start”
b = “hd”
c = “ar”
a.index(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
a.index(c)
2
(3)str.count()
Help on method_descriptor:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
计算子字符串在指定字符串中出现的次数
实例:
a = “start”
b = “a”
a.count(b)
1
(4)str.endswith()
Help on method_descriptor:
endswith(...)
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
如果字符串含有指定的后缀返回True,否则返回False
实例:
a = “start”
b = “a”
a.endswith(b)
False
a = “start”
b = “t”
a.endswith(b)
True
3.字符串内建函数-替换函数
(1)str.replace(old,new)
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
将old替换为new
实例:
a = “start”
b = “op’
a.replace(‘art’, b)
‘stop’
(2)str.strip(char)
Help on method_descriptor:
strip(...)
S.strip([chars]) -> str
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
在str的开头和结尾删除char,当char为空时,默认删除空白符
实例:
a = “ start ”
a.strip()
“start”
(3)str.rstrip()
Help on method_descriptor:
rstrip(...)
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
删除str字符串末尾的空格,或换行符号
实例:
a = “ start ”
a.rstrip()
“ start”
python字符串常用内建函数总结的更多相关文章
- Python—字符串常用函数
Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...
- python字符串常用内置方法
python字符串常用内置方法 定义: 字符串是一个有序的字符的集合,用与存储和表示基本的文本信息. python中引号中间包含的就是字符串. # s1='hello world' # s2=&quo ...
- python 字符串常用操作方法
python 字符串常用操作方法 python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等 1.去除空格 str.strip():删除字符串两边的指定字符,括号的写 ...
- python字符串常用操作方法
python字符串操作常用操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下. 1.去除空格str.strip():删除字符串两边的指定字符,括号的写入指定字符,默 ...
- python - 字符串的内建函数
# -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_3_str_内建函数.py@ide: PyCharm Commu ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- Python 字符串常用判断函数
判断字符串常用函数: S代表某字符串 S.isalnum() 所有字符都是数字或字母,为真返回Ture,否则返回False S.isalha() 所有字符都是字母,为真返回Ture,否则返回 ...
- python字符串常用的方法解析
这是本人在学习python过程中总结的一些关于字符串的常用的方法. 文中引用了python3.5版本内置的帮助文档,大致进行翻译,并添加了几个小实验. isalnum S.isalnum() -> ...
- Python 字符串常用函数
操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...
随机推荐
- 微服务学习笔记二:Eureka服务注册发现
Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...
- sql:无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲突。
--无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之间的排序规则冲 ...
- scss-字符串连接符
+ 运算可用于连接字符串: // SCSS p { cursor: e + -resize; } // 编译后的 CSS 样式 p { cursor: e-resize; } 请注意,如果带引号的字符 ...
- 编写可维护的 Gruntfile.js
load-grunt-tasks 插件 首先介绍下 load-grunt-tasks 这个插件. 我们一般都会把所有用到的插件以及插件的配置写到 Gruntfile.js 里面,对于小项目来说这个文件 ...
- SpagoBI 和 开源ERP(iDempiere)整合入门
Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...
- 我的书单mybooklist
首先自我介绍一下,我是一名程序员. 计算机的世界太浩瀚,而我太过渺小. 比计算机的世界更为广大的是,书籍的海洋.学海无涯. 无论是计算机的世界,还是其他类书的世界,人一辈子要看的书看也看不完. 于是我 ...
- HTML 5入门知识(一)
了解HTML 5 HTML5 并非仅仅用来表示web内容,它的使命是将web带入一个成熟的应用平台,在这个平台上,视频.音频.图像.动画,以及与电脑的交互都被标准化. HTML 5概述 HTML 5实 ...
- 【转】JavaScript 中值得注意的 for 循环
在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...
- 管理uWSGI服务器
管理uWSGI服务器 官网参考 如果您正在管理多个应用程序或高容量站点,请查看 uwsgi皇帝-多应用程序部署 虫族模式 UWSGI订阅式服务器 启动uwsgi服务器 以系统管理员身份启动 uwsgi ...
- Siebel计划和实施
1.计划: 自上而下计划配置项目 1)首先,确定UI和应用产品功能 2)然后,确定为实现UI功能而需要在业务层所做的更改 3)最后,确定为实现业务层更改而需要在数据层所做的更改---尽可能少做更改 如 ...