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() 全 ...
随机推荐
- SVN服务器在Ubuntu16.04下搭建多版本库详细教程
1 介绍 Subversion是一个自由,开源的版本控制系统,这个版本库就像一个普通的文件服务器,不同的是,它可以记录每一次文件和目录的修改情况.这样就可 以很方面恢复到以前的版本,并可以查看数据 ...
- AE多用户同时编辑同一个版本数据的解决方法
项目中做了入库的功能,测试一切正常,但是实际使用多个用户同时编辑default版本的时候,问题就来了,StopEditing 错误信息如下 FDO_E_VERSION_REDEFINED -21472 ...
- Eclipse Configuration
*** Date: 2013年9月12日星期四中国标准时间上午8时41分50秒 *** Platform Details: *** System properties:applicationXMI=o ...
- keychains
keychain在ios中是保存在sqlite数据库中的.这个数据库文件的位置:真机:/private/var/Keychains/keychain-2.db虚拟机:/Users/USER-HOME/ ...
- JSTL标签概述
什么是JSTL JSP 标准标记库(JSP Standard Tag Library,JSTL)是一个实现 Web 应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管理格式化. ...
- 多线程 更新 winform 控件的值,以避免UI线程的卡顿
委托 private delegate void UpdateDGV_AddRes_CallBack(Int32 i,bool Res); 函数实现 private void UpdateDGV_De ...
- SQL 创建自定义方法,匹配正则
C#代码 using System.Text.RegularExpressions; public class FunctionRegex { [Microsoft.SqlServer.Server. ...
- Flask入门 flask结构 url_for 重定向(一)
Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ubuntu系统 sudo apt-get install python-virt ...
- Selenium2学习(十一)-- select下拉框
本篇以百度设置下拉选项框为案例,详细介绍select下拉框相关的操作方法. 一.认识select 1.打开百度-设置-搜索设置界面,如下图所示 2.箭头所指位置,就是select选项框,打开页面 ...
- IEEP-网络设计
IEEP-网络设计 网络设计概述 网络设计概述 1.负责把网络规划阶段获得的客户需求运用技术手段予以规范化体现 2.网络设计一般遵循模块化指导方针,分模块进行设计 3.网络设计的输出成果必须是规范的. ...