python基础之字符串内建函数(部分)
2018-04-03 20:44
381 查看
# -*- coding: utf-8 -*- """ @file:strstudy.py @time:2018/4/2 21:45 @author:xq @contact:xiaoq_xiaoq@163.com """ #把字符串的第一个字符大写 # a = 'test' # print(a.capitalize()) #'Test' #原字符串居中,并使用空格填充至长度 width 的新字符串 # a='center' # print(a.center(10)) # 'center' #返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数 # a='hello' # print(a.count('l')) #2 #检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False. # a='mytest' # print(a.endswith('t')) # print(a.endswith('m')) #True #False #如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False # a= 'test' # b='12test' # c='a!' # print(a.isalpha()) # print(b.isalpha()) # print(c.isalpha()) #True #False #False #如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False # a= 'test' # b='12test' # c='a!' # print(a.isalnum()) # print(b.isalnum()) # print(c.isalnum()) #True #True #False #如果 string 只包含十进制数字则返回 True 否则返回 False. # a='123' # b='12a' # c='12!' # print(a.isdecimal()) # print(b.isdecimal()) # print(c.isdecimal()) #True #False #False #如果 string 只包含数字则返回 True 否则返回 False. # a='123' # b='12a' # c='12!' # print(a.isdigit()) # print(b.isdigit()) # print(c.isdigit()) #True #False #False #如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False # a='asd' # b='ABC' # c='ABd' # d='123' # print(a.islower()) # print(b.islower()) # print(c.islower()) # print(d.islower()) # True # False # False # False #如果 string 中只包含空格,则返回 True,否则返回 False. # a= 'hello world' # b=' ' # print(a.isspace()) # print(b.isspace()) # False # True #如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False # a='AGBF' # b='Af' # print(a.isupper()) # print(b.isupper()) # True # False #以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串 # a='*' # myseq=['1','3','5','6'] # print(a.join(myseq)) # '1*3*5*6' #返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串 # a='left' # print(a.ljust(10)) # 'left ' #转换 string 中所有大写字符为小写. # a='MyTEs' # print(a.lower()) # 'mytes' #截掉 string 左边的空格 # a=' hello ' # print(a.lstrip()) # 'hello ' #返回字符串 str 中最大的字母 # a='test' # print(max(a)) # 't' #返回字符串 str 中最小的字母 # a='test' # print(min(a)) # 'e' #从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str), # 如果 string 中不包含str 则 string_pre_str == string. # a = "http://www.w3cschool.cc/" # print(a.partition("://")) # ('http', '://', 'www.w3cschool.cc/') #把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次. # a='hello world this is a test ' # print(a.replace(' ','%20')) # 'hello%20world%20this%20is%20a%20test%20' #类似于 find()函数,不过是从右边开始查找. # a='test' # print(a.find('t')) # print(a.rfind('t')) #0 # 3 #类似于 index(),不过是从右边开始. # a='test' # print(a.rindex('t')) # 3 # 删除 string 字符串末尾的空格. # a=' hello ' # print(a.rstrip()) # ' hello' # 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串 # a='test,test1,test2' # print(a.split(',')) # ['test', 'test1', 'test2'] #检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查. # a='test' # print(a.startswith('t')) # True #在 string 上执行 lstrip()和 rstrip() # a=' hello ' # print(a.strip()) # 'hello' #翻转 string 中的大小写 # a='MytesT' # print(a.swapcase()) # 'mYTESt' # 如果 string 是标题化的(见 title())则返回 True,否则返回 False # a='The Name' # b='The name' # print(a.istitle()) # print(b.istitle()) # True # False #返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle()) # a='hello world' # print(a.title()) # 'Hello World' #转换 string 中的小写字母为大写 # a='test' # print(a.upper()) # 'TEST' #返回长度为 width 的字符串,原字符串 string 右对齐,前面填充0 # a='test' # print(a.zfill(10)) # '000000test'
相关文章推荐
- Python3基础 str str()将整数,小数转换成字符串。 输入一个小数,显示整数部分与小数部分
- Python修炼--常用字符串、数学函数 分类: python基础学习 2013-06-16 13:23 205人阅读 评论(0) 收藏
- Python字符串部分函数(上)
- python基础--字符串常用函数
- Python基础入门部分函数用法
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数
- python基础笔记2-字符串处理函数
- Python基础(函数部分)-day04
- Python入门笔记(18):Python函数(1):基础部分
- 《转》Python学习(17)-python函数基础部分
- python学习之基础知识部分
- python基础教程共60课-第13课字符串
- 第二部分 函数技术的基础
- [Python入门及进阶笔记]Python-基础-函数小结
- 第20课 Python字符串高级函数
- [Python学习] 专题一.函数的基础知识
- python split()函数使用拆分字符串 将字符串转化为列表
- Python基础语法之字符串
- Python内置的字符串处理函数整理
- Python 字符串的内建函数