Python str内部功能介绍
2016-04-30 00:24
393 查看
def capitalize(self):
str = 'aGe' print(str.capitalize()) 结果:Age 结论:首字母大写,其他字母都小写
def casefold(self):
str = 'AGE-age' print(str.casefold())
结果:age-age 结论:首字母大写,其他字母都小写
def center(self, width, fillchar=None):
str = 'AGE-age' print(str.center(20,'=')) 结果:======AGE-age======= 结论:字符串str放在长为20的字符中,缺少的用fillchar字符来填充
def count(self, sub, start=None, end=None):
str = 'AGE-age' print(str.count('ag')) print(str.count('ag',5,6)) 结果:1、0 结论:统计在字符串中出现特定字符的次数,也可以指定起始位置
def encode(self, encoding='utf-8', errors='strict'):
def endswith(self, suffix, start=None, end=None):
str = 'chian' print(str.endswith('an',3,5)) 结果:True 结论:返回是否以特定字符结尾的字符串,可以指定起始位置
def expandtabs(self, tabsize=None):
str = "\talex" print (str.expandtabs()) print(str.expandtabs(10)) 结果: alex/ alex 结论:缺省的tab键是8个空格,加数字为数字位的空格
def find(self, sub, start=None, end=None):
str = "alex is a workhard man" print (str.find('is')) print (str.find('or',4,13)) 结果:5/11 结论:在字符串中查找字符,可以指定起始字符
def format(*args, **kwargs):
def index(self, sub, start=None, end=None):
str = "alex is a workhard man" print (str.index('is'))
结果:5 结论:在字符串中查找字符位置,可以指定起始字符
def isspace(self):
#-*- coding:UTF-8 -*- str = ' fa' print(str.isspace())
结果:False 结论:要求字符串都是空串
def istitle(self):
str = "This Is String Example...Wow!!!" print (str.istitle()) str = "This is string example....wow!!!" print (str.istitle()) 结果:True/False 结论:只有首字母都是大写才是TITLE
def join(self, iterable):
str = "This Is String Example...Wow!!!" print (':'.join(str)) str1 = ['This','is','a','book'] print (':'.join(str1)) str2 = ('This','is','a','book') print (':'.join(str2)) str3 = {'1':'This','2':'is','3':'a','4':'book'} print ('-'.join(str3)) 结果:
T:h:i:s: :I:s: :S:t:r:i:n:g: :E:x:a:m:p:l:e:.:.:.:W:o:w:!:!:!
This:is:a:book
This:is:a:book
2-4-1-3
结论:用连接符把字符连接起来
def ljust(self, width, fillchar=None):
str1 = 'This is a book' print (str1.ljust(20,'#')) 结果:This is a book###### 结论:左对齐,默认填充字符为空
def maketrans(self, *args, **kwargs):
import string str1 = 'abcde' map = str1.maketrans('abcde','12345') print (type(map)) print (map) 结果:
<class 'dict'>
{97: 49, 98: 50, 99: 51, 100: 52, 101: 53}结论:ASCII a-97,1-49 def partition(self, sep):
import string str = "http://www.w3cschool.cc/" print (str.partition('//'))
结果:('http:', '//', 'www.w3cschool.cc/')结论:返回三个字符串,左边是分隔符左边的,右边是分隔符右边的,中间是分割符
def replace(self, old, new, count=None):
str = "http://www.w3cschool.cc//" print (str.replace('//','$$',1)) print (str.replace('//','$$',2)) 结果:http:$$www.w3cschool.cc// http:$$www.w3cschool.cc$$
结论:用新字符替换这些旧的字符
def rfind(self, sub, start=None, end=None):
str = "hello,alex,alpha" print (str.rfind('al',5,13)) 结果:11 结论:找到最右边的这个字符,并且返回索引号
def splitlines(self, keepends=None):
str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"; print (str.splitlines( )) print (str.splitlines( 0 )) print (str.splitlines( 3 )) print (str.splitlines( 4 )) print (str.splitlines( 5 )) 结果:
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d']
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']结论:返回一个包含各行作为元素的列表
相关文章推荐
- python 多线程 join 的 细节问题 注意使用事项
- Python开发环境配置
- 如何在MAC 指定文件夹打开终端(terminal)
- python 函数超时装饰器
- python 正则表达式
- python web中models.py中的一些处理
- win下python和opencv安装
- python解决网站的反爬虫策略
- python3.5使用pip安装scrapy时unable to find vcvarsall.bat异常处理
- knn算法python实现
- python解决八皇后问题
- Regular Expression Python
- 基于python的爬虫---自顶向下的设计思想
- python学习5
- windows环境下把Python代码打包成独立执行的exe可执行文件
- python算法基础
- 武sirpython第二讲(1)运算符
- sublime text3里搭建python
- Python生成二维码脚本
- python pachong zhuanzai