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() 全 ...
随机推荐
- ubuntu系统没有声音解决方法
好像装了个放视频的软件,就没有声音了.后面网上搜到了一个简单粗暴的办法,效果很明显,改变权限后直接就有声音了. -------------------------------------------- ...
- spring cloud provider报“Error parsing HTTP request header”,feign端报“Read timed out“
这两天在调试spring cloud feign+hystrix报了如下错误: spring cloud provider报“Error parsing HTTP request header”,fe ...
- angular2-生命周期钩子函数
生命周期的顺序 当Angular使用构造函数新建一个组件或指令后,就会按下面的顺序在特定时刻调用这些生命周期钩子方法: 钩子 目的和时机 ngOnChanges() 当Angular(重新)设置数据绑 ...
- Python进阶篇四:Python文件和流
摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...
- MongoDB(online) 优化
MongoDB(online) 优化 1. find.findOne 2. 操作 vip_emp_relation 的一个公共方法 3. 查询记录数 4. save.insert 5. 总结 1. f ...
- Vue项目中引入mockjs
前提:创建好的vue项目 前言: 为什么引入mockjs:为了实现前后端分离,开发工作可以异步进行 其他工具:axios 一般的前后端交互过程:前端 --> ajax请求 --> 网络协议 ...
- TinkPHP去重统计查询
当统计一个有重复的字段可以用这个方法 $count = $model->where($map)->count('distinct(id)'); 转自 http://www.thinkphp ...
- apache-实战(一)
Apache 1.html的完整格式 # vim /var/www/html/index.html<html><head><title>我要</title& ...
- Android开发最佳学习路线图(转)
Android开发总体路线图: 基础学习——JavaSE: 很多朋友一上手就开始学习Android,似乎太着急了一些. Android应用程序开发是以Java语言为基础的,所以没有扎实 ...
- python接口测试-项目实践(二)获取接口响应,取值(re、json)
一 分别请求3个接口,获取响应. 第三方接口返回有两种:1 纯字符串 2 带bom头的json字串 import requests api1 = 'url1' response1 = request ...