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() 全 ...
随机推荐
- POJ 3667 线段树区间合并
http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...
- Jmeter(一)http接口添加header和cookie --转载
Jmeter(一)http接口添加header和cookie HTTP信息头管理器在Jmeter的使用过程中起着很重要的作用,通常我们在通过Jmeter向服务器发送http请求(get或者post ...
- SQL 出现18456
SQL Server 2008R2 18456错误解决方案 SQL Server 2008R2 18456错误解决方案 微软解释说,因密码或用户名错误而使身份验证失败并导致连接尝试被拒时,类似下面 ...
- JS教程之实现加载图片时百分比进度
思路:思路其实很简单,ajax执行时,会生成一个event对象,其中会包含要加载的文件的大小和当前已经加载完成部分的大小,通过这两个值即可计算出百分比 事件介绍onprogress 当浏览器正在加载媒 ...
- static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
查看HashMap源码,发现这个static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; 值为16. 这个是位移算法. 例如: 4<& ...
- Perl 基础笔记: 使用 cpanm 安装 Perl 模块
cpanm 其实只是一个可执行文件而已.将它下载到 bin 目录,然后添加执行权限就可以用了. $ sudo wget http://xrl.us/cpanm -O /usr/bin/cpanm; s ...
- SQA和测试规程
SQA *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; ...
- IOS 即时通讯的框架 配置环境
一.了解XMPP 协议(标准)XMPP 即时通讯协议SGIP 短信网关协议 这手机发短信 移动支付和网页支付 0x23232[0,1] 0x23232 0x23232 0x23232 只有协议,必须会 ...
- Android(java)学习笔记23:finally关键字的作用
1. finally 关键字的作用 package cn.itcast_07; import java.text.ParseException; import java.text.SimpleDate ...
- UNIX PIPES 管道原稿
40年前,Unix操作系统横空出世,Unix不仅仅带来了一个操作系统,还创造C语言,Socket,开源,黑客等等文化,这些文化影响着整个计算机世界的文明,直到今天. 如果说Unix是计算机文明中最伟大 ...