python3.x 基础一:str字符串方法
*字符串不能更改值
数据类型字符串str
- | capitalize(...) 返回字符串中第一个字母大写
| S.capitalize() -> str
|
| Return a capitalized version of S, i.e. make the first character
| have upper case and the rest lower case.>>> str1='i am chinese'
>>> str1.capitalize()
'I am chinese' - | casefold(...) 将大写转换成小写
| S.casefold() -> str
|
| Return a version of S suitable for caseless comparisons.>>> str2='I AM A CHINESE'
>>> str2.casefold()
'i am a chinese' - | center(...) 第一个参数是总字符长度,第二个是补全字符,原字符串居中
| S.center(width[, fillchar]) -> str
|
| Return S centered in a string of length width. Padding is
| done using the specified fill character (default is a space)>>> str1.center(50,'#')
'###################i am chinese###################' - | 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.>>> str1.count('e')
2
>>> str1.count('i')
2
>>> str1.count('a')
1 - | 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.>>> str2.endswith('SE')
True
>>> str2.endswith('S')
False>>> str = "this is string example....wow!!!";
>>> suffix = "is"
>>> print (str.endswith(suffix, 2, 6))
False
>>> print(str[2:7])
is is
#字符串含有指定后缀,为什么不是返回真?
#理解错误,起始位置是左闭右开
>>> print (str.endswith(suffix, 2,7))
True - | expandtabs(...) 扩展tab字符n个
| S.expandtabs(tabsize=8) -> str
|
| Return a copy of S where all tab characters are expanded using spaces.
| If tabsize is not given, a tab size of 8 characters is assumed.>>> str4='I am \t Chinese'
>>> str4.expandtabs(10)
'I am Chinese' - | find(...) 返回查找到的第一个字符位置,否则返回-1
| 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.>>> str2
'I AM A CHINESE'
>>> str2.find('M')
3
>>> str2.find('A')
2
>>> str2.find('B')
-1 - | index(...) 同上,查找不到则报错
| S.index(sub[, start[, end]]) -> int
|
| Like S.find() but raise ValueError when the substring is not found.>>> str2
'I AM A CHINESE'
>>> str2.index('A')
2
>>> str2.index('B')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found - | isalnum(...) 判断字符串是否由字母数字组成
| S.isalnum() -> bool
|
| Return True if all characters in S are alphanumeric
| and there is at least one character in S, False otherwise.>>> str5='I am 1 chinese'
>>> str5.isalnum()
False
>>> str6='iAm1Chinese'
>>> str6.isalnum()
True - | isalpha(...) 判断字符串是否全部是字符组成
| S.isalpha() -> bool
|
| Return True if all characters in S are alphabetic
| and there is at least one character in S, False otherwise.>>> str7='iamChinese'
>>> str7.isalpha()
True
>>> str3.isalpha()
False - | isdecimal(...) 如果字符串是否只包含十进制字符返回True,否则返回False
| S.isdecimal() -> bool
|
| Return True if there are only decimal characters in S,
| False otherwise.>>> str8=u'asdfasdf1000'
>>> str8.isdecimal()
False
>>> stru=u'1000'
>>> stru.isdecimal()
True - | isdigit(...) 判断字符串中是否全部是数字
| S.isdigit() -> bool
|
| Return True if all characters in S are digits
| and there is at least one character in S, False otherwise.>>> str9,str10='abc123','123'
>>> str9.isdigit()
False
>>> str10.isdigit()
True - | isidentifier(...) 判断变量明明是否合法
| S.isidentifier() -> bool
|
| Return True if S is a valid identifier according
| to the language definition.
|
| Use keyword.iskeyword() to test for reserved identifiers
| such as "def" and "class". - | islower(...) 判断字符串中所有字母是否小写
| S.islower() -> bool
|
| Return True if all cased characters in S are lowercase and there is
| at least one cased character in S, False otherwise.>>> str11,str12,str13='abcd','AbCd','Ab123'
>>> str11.islower()
True
>>> str12.islower()
False
>>> str13.islower()
False
>>> str14='aa123'
>>> str14.islower()
True - | isnumeric(...) 判断字符串中所有字符是否是数字
| S.isnumeric() -> bool
|
| Return True if there are only numeric characters in S,
| False otherwise.>>> str10,str14
('123', 'aa123')
>>> str10.isnumeric()
True
>>> str14.isnumeric()
False - | isspace(...) 判断字符是否是空格
| S.isspace() -> bool
|
| Return True if all characters in S are whitespace
| and there is at least one character in S, False otherwise.>>> str1,str14,str15
('i am chinese', 'aa123', ' ')
>>> str1.isspace(),str14.isspace(),str15.isspace()
(False, False, True) - | istitle(...) 判断字符串是否是标题,标题单词首字母均大写
| S.istitle() -> bool
|
| Return True if S is a titlecased string and there is at least one
| character in S, i.e. upper- and titlecase characters may only
| follow uncased characters and lowercase characters only cased ones.
| Return False otherwise.>>> str16,str17='I Am Chinese','I am chinese'
>>> str16.istitle(),str17.istitle()
(True, False) - | isupper(...) 判断字符串中字母是否全部是大写
| S.isupper() -> bool
|
| Return True if all cased characters in S are uppercase and there is
| at least one cased character in S, False otherwise.>>> str18,str19='ABC123','Abc123'
>>> str18.isupper(),str19.isupper()
(True, False) - | join(...) 将其他可迭代对象转换成字符串,S表示字符串分隔符
| S.join(iterable) -> str
|
| Return a string which is the concatenation of the strings in the
| iterable. The separator between elements is S.>>> list1=['a','b','c','d']
>>> ','.join(list1)
'a,b,c,d'
>>> ''.join(list1)
'abcd'
>>> ' '.join(list1)
'a b c d' - | ljust(...) 共width个位置,字符串靠左,右边以字符填充
| S.ljust(width[, fillchar]) -> str
|
| Return S left-justified in a Unicode string of length width. Padding is
| done using the specified fill character (default is a space).>>> str10
'123'
>>> str10.ljust(10,'#')
'123#######' - | lower(...) 将字符串中所有大写字母转换成小写
| S.lower() -> str
|
| Return a copy of the string S converted to lowercase. - | lstrip(...) 将左边空格剪掉,可以指定其他被剪掉空格,\n \t
| S.lstrip([chars]) -> str
|
| Return a copy of the string S with leading whitespace removed.
| If chars is given and not None, remove characters in chars instead.>>> str20=' 123abc ABC '
>>> str20.lstrip()
'123abc ABC 'strip()将左右两边空格剪掉,rstrip()将右边空格剪掉
- | partition(...) 以某个字符切片字符串,返回三元组,中间是分隔符,找不到则返回源字符串和2个空格
| S.partition(sep) -> (head, sep, tail)
|
| Search for the separator sep in S, and return the part before it,
| the separator itself, and the part after it. If the separator is not
| found, return S and two empty strings.>>> str21='http://www.baidu.com'
>>> str21.partition('://'... )
('http', '://', 'www.baidu.com')
>>> str21.partition('abc')
('http://www.baidu.com', '', '') - | 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.>>> str21
'http://www.baidu.com'
>>> str21.replace('baidu','jd')
'http://www.jd.com'
>>> str21.replace('w','z')
'http://zzz.baidu.com'
>>> str21.replace('w','z',1)
'http://zww.baidu.com' - | rfind(...) 从左往右查找,并返回最右边的字符索引位置
| S.rfind(sub[, start[, end]]) -> int
|
| Return the highest 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.>>> str21
'http://www.baidu.com'
>>> str21.rfind('b')
11
>>> str21.rfind('w')
9 - | rindex(...)
| S.rindex(sub[, start[, end]]) -> int
|
| Like S.rfind() but raise ValueError when the substring is not found. - | rjust(...) 字符靠右,左边补全字符,默认空格
| S.rjust(width[, fillchar]) -> str
|
| Return S right-justified in a string of length width. Padding is
| done using the specified fill character (default is a space).| rjust(...)
| S.rjust(width[, fillchar]) -> str
|
| Return S right-justified in a string of length width. Padding is
| done using the specified fill character (default is a space).>>> str10.rjust(10,'x')
'xxxxxxx123' - | rpartition(...) 无法找到切片字符串,则源字符串索引是2,前面是空格
| S.rpartition(sep) -> (head, sep, tail)
|
| Search for the separator sep in S, starting at the end of S, and return
| the part before it, the separator itself, and the part after it. If the
| separator is not found, return two empty strings and S.>>> str21.rpartition('://')
('http', '://', 'www.baidu.com')
>>> str21.rpartition('abc')
('', '', 'http://www.baidu.com') - | split(...) 默认分隔符空格 \t \n,可以指定分割次数
| S.split(sep=None, maxsplit=-1) -> list of strings
|
| Return a list of the words in S, using sep as the
| delimiter string. If maxsplit is given, at most maxsplit
| splits are done. If sep is not specified or is None, any
| whitespace string is a separator and empty strings are
| removed from the result.>>> str21.split('w')
['http://', '', '', '.baidu.com']
>>> str21.split('w',1)
['http://', 'ww.baidu.com']
>>> str21.split('w',2)
['http://', '', 'w.baidu.com']
>>> str21.split('w',3)
['http://', '', '', '.baidu.com'] - | rsplit(...) 右边第一个分割符开始切分,默认全切分,可以指定分割次数
| S.rsplit(sep=None, maxsplit=-1) -> list of strings
|
| Return a list of the words in S, using sep as the
| delimiter string, starting at the end of the string and
| working to the front. If maxsplit is given, at most maxsplit
| splits are done. If sep is not specified, any whitespace string
| is a separator.>>> str21.rsplit('w')
['http://', '', '', '.baidu.com']
>>> str21.rsplit('w',1)
['http://ww', '.baidu.com']
>>> str21.rsplit('w',2)
['http://w', '', '.baidu.com']
>>> str21.rsplit('w',3)
['http://', '', '', '.baidu.com'] - | splitlines(...) 行分割
| S.splitlines([keepends]) -> list of strings
|
| Return a list of the lines in S, breaking at line boundaries.
| Line breaks are not included in the resulting list unless keepends
| is given and true.>>> str22='www\nbaidu\ncom'
>>> str22.splitlines()
['www', 'baidu', 'com'] - | startswith(...) 同endswith()
| S.startswith(prefix[, start[, end]]) -> bool
|
| Return True if S starts with the specified prefix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| prefix can also be a tuple of strings to try. - | swapcase(...) 字符串中大小写转换,大写变小写,小写变大写--- upper(),lower()
| S.swapcase() -> str
|
| Return a copy of S with uppercase characters converted to lowercase
| and vice versa.>>> str23='asdf123'
>>> str23.swapcase()
'ASDF123'
>>> str24='ABcd222'
>>> str24.swapcase()
'abCD222' - | title(...) 返回一个titile标题
| S.title() -> str
|
| Return a titlecased version of S, i.e. words start with title case
| characters, all remaining cased characters have lower case.>>> str25='a good persion asdf 213'
>>> str25.title()
'A Good Persion Asdf 213' - | zfill(...) 总长width个字符,前面补0
| S.zfill(width) -> str
|
| Pad a numeric string S with zeros on the left, to fill a field
| of the specified width. The string S is never truncated.>>> str25.zfill(30)
'0000000a good persion asdf 213'
python3.x 基础一:str字符串方法的更多相关文章
- Python基础7:字符串方法
1 * 重复输出字符串 print('helo '*4) 2 [],[:] 通过索引获取字符串中的字符,这里和列表中的切片操作是相同的,具体内容见列表 print('hello word'[2:]) ...
- Python基础(数字,字符串方法)
数字: #二进制转十进制 a=' v=int(a,base=2) print(v) 进制转换 #当前数字的二进制至少有多少位 b=2 v2=b.bit_length() print(v2) 数值二进制 ...
- Python基础数据类型str字符串
3.3字符串str ' ' 0 切片选取 [x:y] 左闭右开区间 [x:y:z] 选取x到y之间 每隔z选取一次(选取x,x+z,....) z为正 索引位置:x在y的左边 z为负 索引位置:x在y ...
- python基础-生成随机字符串方法
python解释器示例 >>> import uuid >>> uuid.uuid1() UUID('ae6822e6-c976-11e6-82e0-0090f5f ...
- python基础学习笔记——字符串方法
索引和切片: 索引:取出数组s中第3个元素:x=s[2] 切片:用极少的代码将数组元素按需处理的一种方法.切片最少有1个参数,最多有3个参数,演示如下: 我们假设下面所用的数组声明为array=[2, ...
- Python字符串str的方法使用
#!usr/bin/env python# -*-coding:utf-8-*-#字符串通常用双引号或单引号来表示:'123',"abc","字符串"#str字 ...
- C#基础之操作字符串的方法
C#基础之操作字符串的方法 C#中封装的对字符串操作的方法很多,下面将常见的几种方法进行总结: 首先定义一个字符串str 1.str.ToCharArray(),将字符串转换成字符数组 2.str.S ...
- 字符串str.format()方法的个人整理
引言: 字符串的内置方法大致有40来个,但是一些常用的其实就那么20几个,而且里面还有类似的用法,区分度高比如isalpha,isalnum,isdigit,还有一些无时不刻都会用到的split切分, ...
- Python基础 第三章 使用字符串(3)字符串方法&本章小结
字符串的方法非常之多,重点学习一些最有用的,完整的字符串方法参见<Python基础教程(第三版)>附录B. 模块string,虽然风头已小,但其包含了一些字符串方法中没有的常量和函数,故将 ...
随机推荐
- 2019-2020-1 20199326《Linux内核原理与分析》第八周作业
可执行程序工作原理## 编译链接的过程### 示例程序hello.c #include<stdio.h> void main() { printf("Hello world\n& ...
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
- java并发中的Synchronized关键词
文章目录 为什么要同步 Synchronized关键词 Synchronized Instance Methods Synchronized Static Methods Synchronized B ...
- Spring Cloud sleuth with zipkin over RabbitMQ教程
文章目录 Spring Cloud sleuth with zipkin over RabbitMQ demo zipkin server的搭建(基于mysql和rabbitMQ) 客户端环境的依赖 ...
- 截取nginx日志
截取nginx日志 sed -n '/24\/Feb\/2017:11:00:00/,/24\/Feb\/2017:12:00:00/p' yunying_api.wanglibao.com.acce ...
- Nginx重写请求后将url?后的参数去除
2019独角兽企业重金招聘Python工程师标准>>> 使用?结尾 注意,关键点就在于"?"这个尾缀.重定向的目标地址结尾处如果加了?号,则不会再转发传递 ...
- JAVA第二次blog总结
JAVA第二次blog总结 0.前言 这是我们在博客园上第二次写博客,进行JAVA阶段学习的总结.现在我们接触到JAVA已经有一段时间了,但难点还是在于编程思想和方法的改变,第二阶段的学习让我对于理解 ...
- 10_CSS入门和高级技巧(8)
图片透明 先来复习一下盒子的透明问题: opacity:0.6; 介于0~1之间,为了让IE兼容,我们要使用IE自己的滤镜: filter:alpha(opacity=60); 盒子的透明,会让里面的 ...
- neo4j在docker容器环境中无法启动的问题
回去过了个周末,neo4j就无法启动了 数据还没备份出来,着急啊.上周回去前刚刚在研究怎么把数据导出来,尝试了一些容器导出的方法,没有成功.周一回来就无法启动了... 表现为启动后过几十秒又变为sto ...
- Mysql常用sql语句(13)- having 过滤分组结果集
测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...