s = "abcaDa a"
s2 = "123a abc ABCSAa s "
s3 = "\tas \t\tb123"
s4 = ' &abc123 c ## '

1.str.capitalize()

将原字符串内的首字母转成大写,其他部分小写,再返回新字符串

print("s.capitalize() = {function}".format(function = s.capitalize())) 

Output:

s.capitalize() = Abcada a

2.str.upper()

将原字符串的字母转为大写

print("s.upper() = {function}".format(function = s.upper()))

Output:

s.upper() = ABCADA A

3.str.lower()

将原字符串的字母转为小写

print("s.lower() = {function}".format(function = s.lower()))

Output:

s.lower() = abcada a

4.str.swapcase()

将原字符串内的大写小写反转

print("s.swapcase() = {function}".format(function = s.swapcase()))

Output:

s.swapcase() = ABCAdA A

5.str.title()

原字符串内如果有特殊字符(包括数字)连接字母,则将特殊字符后的首个英文字母转化为大写形态,并返回新字符串

print("s2.title() = {function}".format(function = s2.title()))

Output:

s2.title() = 123A Abc Abcsaa S

6.str.center()

str.center(宽度,填充字符) 将字符串以居中的格式返回,若宽度值比len(s)小则返回原字符串,填充以从左到右为规则,填充字符的默认值为空格,值可以自己更改

print("s2.center() = {function}".format(function = s2.center(19,'&')))
print("s2.center() = {function}".format(function = s2.center(20,'&')))

Output:

#s2 = 123a abc ABCSAa s
s2.center() = &123a abc ABCSAa s
s2.center() = &123a abc ABCSAa s &

7.str.expandtabs()

str.expandtabs(tabsize = 8) 将原字符串中\t以前的字符补满8位(默认),tabsize的值从0-7即8位,在0-7中任意取值则默认tabsize = 8,此后

往上+1,就相当于增加一个空格

print("s3.expandtabs ={function}".format(function = s3.expandtabs()))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(0)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(5)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(8)))
print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))

Output:

#s3 = "\tas \t\tb123"
s3.expandtabs = as b123
s3.expandtabs =as b123
s3.expandtabs = as b123
s3.expandtabs = as b123
s3.expandtabs = as b123

8.String_len = len(str)

公共方法,计算字符串的长度

print("s2_length = {0}".format(len(s2))) 

Output:

s2_length = 18

9.str.startswith()

str.startswith(substr,strbeg,strend) 判断原字符串是否以子字符串开头 可以用切片的形式进行判断(以顾头不顾尾为原则),这里的strend要比strbeg大否则传回false 函数结果返回一个布尔值

print("s.startswith() = {function}".format(function = s.startswith('ab')))
print("s.startswith() = {function}".format(function = s.startswith('D',2)))
print("s.startswith() = {function}".format(function = s.startswith('c',2,5)))
print("s.startswith() = {function}".format(function = s.startswith('c',2,100)))

Output:

#s = abcaDa a
s.startswith() = True
s.startswith() = False
s.startswith() = True
s.startswith() = True

10.str.endswith()

str.endswith(substr,strbeg,strend) 判断字符串是否以子字符串结尾 结果返回布尔值,这里的strend要比strbeg小否则传回false 和startswith()一样要从左到右为原则

print("s.endswith() = {function}".format(function = s.endswith('Da a')))
print("s.endswith() = {function}".format(function = s.endswith('a',-1)))
print("s.endswith() = {function}".format(function = s.endswith('Da a',-4)))
print("s.endswith() = {function}".format(function = s.endswith('c',-8,-5)))
print("s.endswith() = {function}".format(function = s.endswith('c',-7,-5)))
print("s.endswith() = {function}".format(function = s.endswith('c',-7,-4)))
print("s.endswith() = {function}".format(function = s.endswith('Da a',-1,6)))

Output:

#s = abcaDa a
s.endswith() = True
s.endswith() = True
s.endswith() = True
s.endswith() = True
s.endswith() = True
s.endswith() = False
s.endswith() = False

11.str.find()

str.find(substr,strbeg,strend) 寻找字符串中是否存在该子字符串并返回其索引,找不到则返回-1 若同时出现相同的字符串则返回最先找到

的子字符串索引,切片(遵循顾头不顾尾原则)

print("s.find() = {function}".format(function = s.find('a')))
print("s.find() = {function}".format(function = s.find('ab')))
print("s.find() = {function}".format(function = s.find('f')))
print("s.find() = {function}".format(function = s.find('b',-8,-5)))
print("s.find() = {function}".format(function = s.find('b',0)))

Output:

s.find() = 0
s.find() = 0
s.find() = -1
s.find() = 1
s.find() = 1

12.str.index()

str.index(substr,strbeg,strend) 与find()的功能大致相同,但子字符串不存在于字符串中就会报错,结果返回索引

print("s.index() = {function}".format(function = s.index('a')))
print("s.index() = {function}".format(function = s.index('ab')))
print("s.index() = {function}".format(function = s.index('D',-8,5)))
print("s.index() = {function}".format(function = s.index('b',0)))

Output:

#s = abcaDa a
s.index() = 0
s.index() = 0
s.index() = 4
s.index() = 1

13.str.strip()

str.strip([chars]) 去掉字符串中首和尾的字符 默认删除的是空格 chars可以自行更改

print("s3.strip() = {function}".format(function = s3.strip()))

Output:

#s3 =    as              b123
s3.strip() = as b123

14.str.rstrip()

str.rstrip([chars]) 去掉字符串右边的字符 默认删除的是空格 返回值为删除后的新字符串

print("s4.rstrip() = {function}".format(function = s4.rstrip()))
print("s4.rstrip() = {function}".format(function = s4.rstrip('# c')))

Output:

#s4 = '    &abc123 c ##    '
s4.rstrip() = &abc123 c ##
s4.rstrip() = &abc123

15.str.lstrip()

str.lstrip([chars]) 去掉字符串左边的字符 默认删除的是空格 返回值为删除后的新字符串

print("s4.lstrip() = {function}".format(function = s4.lstrip()))
print("s4.lstrip() = {function}".format(function = s4.lstrip(' &')))

Output:

#s4 = '    &abc123 c ##    '
s4.lstrip() = &abc123 c ##
s4.lstrip() = abc123 c ##

16.str.count()

str.count(substr,start,end) start为第一个字符,这里默认为0 end未结束搜索的位置,默认为len(s) 主要功能统计字符串中某个字符出现

的个数并返回个数

print("s.count() = {function}".format(function = s.count('a')))
print("s.count() = {function}".format(function = s.count('Fa')))
print("s.count() = {function}".format(function = s.count('a',-7)))

Output:

#s = "abcaDa a"
s.count() = 4
s.count() = 0
s.count() = 3

17.str.split()

str.split(substr = " ",num = s.count(str)) 主要作用分割字符串 这里的substr默认以空格分割,num为总共切割的次数,若num有数值怎分割num+1个字符串 分割后将字符串转化为list形式 substr会在分割后消失

print("s2.split() = {function}".format(function = s2.split()))
print("s2.split() = {function}".format(function = s2.split(' ',0)))
print("s2.split() = {function}".format(function = s2.split('a',1)))
print("s2.split() = {function}".format(function = s2.split('a',-1)))

Output:

#s2 = "123a abc ABCSAa s "
s2.split() = ['123a', 'abc', 'ABCSAa', 's']
s2.split() = ['123a abc ABCSAa s ']
s2.split() = ['', ' abc ABCSAa s ']
s2.split() = ['', ' ', 'bc ABCSA', ' s ']

18.str.format()

三种用法:

print("s = {}|s2 = {}|s3 = {}".format(s,s2,s3))
print("s = {0}|s2 = {1}|s3 = {2}|s = {0}|s3 = {2}".format(s,s2,s3))
print("s = {s}{sep}s2 = {s2}{sep}s3 = {s3}".format(s = s,s2 = s2,s3 = s3,sep = '|'))

Output:

s = abcaDa a|s2 = 123a abc ABCSAa s |s3 =       as              b123
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123|s = abcaDa a|s3 = as b123
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123

当占位符不按顺序写会报错 tuple index out of range

19.str.replace()

s.replace(old,new,max) old为将被替换的字符串,new为替换old的字符串,max为替换次数将字符串进行替换,若old未存在原字符串内则返回原

字符串

print("s.replace() = {function}".format(function = s.replace('ac','A')))
print("s.replace() = {function}".format(function = s.replace('a','A')))
print("s.replace() = {function}".format(function = s.replace('a','A',2)))

Output:

#s = "abcaDa a"
s.replace() = abcaDa a
s.replace() = AbcADA A
s.replace() = AbcADa a

20.is函数

  str.isalnum()  #判断是否由数字或字母组成

  str.isalpha()    #判断是否含有字母

  str.isdecimal()  #判断是否含有十进制数字

  str.isdigit()  #判断是否含有数字

  str.islower()  #判断是否含有小写字母

  str.isupper()  #判断是否含有大写字母

  str.isnumeric()  #判断是否只包含数字字符

  str.isspace()  #判断是否只含有空格

  str.istitle()  #判断是否经过title()函数处理过后的标题

21.join()

str.join(sequence)  sequence为要连接的元素序列,函数作用是将指定字符串与原字符串中每一个字符一一连接

s = 'alex'
print('&&'.join(s)) #a&&l&&e&&x
l = ['a','l','e','x']
print('+'.join(l)) #a+l+e+x
t = ('a','b')
print('*'.join(t)) #a*b

PS:String索引对应

 

Python学习日记(一) String函数使用的更多相关文章

  1. python学习日记(生成器函数进阶)

    迭代器和生成器的概念 迭代器 对于list.string.tuple.dict等这些容器对象,使用for循环遍历是很方便的.在后台for语句对容器对象调用iter()函数.iter()是python内 ...

  2. Python 学习日记(第三周)

    知识回顾 在上一周的学习里,我学习了一些学习Python的基础知识下面先简短的回顾一些: 1Python的版本和和安装 Python的版本主要有2.x和3.x两个版本这两个版本在语法等方面有一定的区别 ...

  3. Python学习笔记之常用函数及说明

    Python学习笔记之常用函数及说明 俗话说"好记性不如烂笔头",老祖宗们几千年总结出来的东西还是有些道理的,所以,常用的东西也要记下来,不记不知道,一记吓一跳,乖乖,函数咋这么多 ...

  4. Python学习日记 --day2

    Python学习日记 --day2 1.格式化输出:% s d  (%为占位符 s为字符串类型 d为数字类型) name = input('请输入姓名') age = int(input('请输入年龄 ...

  5. python学习日记(基础数据类型及其方法01)

    数字 int 主要是用于计算的,常用的方法有一种 #既十进制数值用二进制表示时,最少使用的位数i = 3#3的ASCII为:0000 0011,即两位 s = i.bit_length() print ...

  6. [python学习] 语言基础—排序函数(sort()、sorted()、argsort()函数)

    python的内建排序函数有 sort.sorted两个. 1.基础的序列升序排序直接调用sorted()方法即可 ls = list([5, 2, 3, 1, 4]) new_ls = sorted ...

  7. Python学习笔记(Ⅱ)——循环/选择/函数

    一.循环结构 python中提供了for循环和while循环两种操作,没有do……while语句. 1.for循环: 与其他语言中for循环的常见的写法如for (int i=0;i<10;i+ ...

  8. Python学习笔记010——匿名函数lambda

    1 语法 my_lambda = lambda arg1, arg2 : arg1 + arg2 + 1 arg1.arg2:参数 arg1 + arg2 + 1 :表达式 2 描述 匿名函数不需要r ...

  9. python学习日记-01

    一. 熟悉 在正式介绍python之前,了解下面两个基本操作对后面的学习是有好处的: (1)基本的输入输出 可以在Python中使用+.-.*./直接进行四则运算. >>> 1+3* ...

随机推荐

  1. C/C++中double类型的比较

    由于double浮点数的精度问题,所以在比较大小的时候,不能像int整数型那样,直接if(a==b),if(a<b),if(a>b) 要使用一个精度EPS: ; //一般这样子就够,但有时 ...

  2. Python面向对象 -- slots, @property、多重继承MixIn、定制类(str, iter, getitem, getattr, call, callable函数,可调用对象)、元类(type, metaclass)

    面向对象设计中最基础的3个概念:数据封装.继承和多态 动态给class增加功能 正常情况下,当定义了一个class,然后创建了一个class的实例后,可以在程序运行的过程中给该实例绑定任何属性和方法, ...

  3. tldr/cheat

    tldr 比man好用的查询命令查询工具, man很强大,但是 TLDR,too long dont read 安装 npm install -g tldr 使用说明 其他版本下载 https://g ...

  4. P1913 L国的战斗之伞兵

    题目链接 P1913 L国的战斗之伞兵 思路 从无风点倒着dfs,本来是道大水题,结果输入的时候第二层循环打错了!手残打成i++ 代码 #include<iostream> #includ ...

  5. 55、Spark Streaming:updateStateByKey以及基于缓存的实时wordcount程序

    一.updateStateByKey 1.概述 SparkStreaming 7*24 小时不间断的运行,有时需要管理一些状态,比如wordCount,每个batch的数据不是独立的而是需要累加的,这 ...

  6. 微信小程序前端promise封装

    config.js const config = { base_url_api : "https://douban.uieee.com/v2/movie/", } export { ...

  7. Uncaught SyntaxError: Unexpected token o

    浏览器报Uncaught SyntaxError: Unexpected token o 这原因是你ajax获取数据的时候对数据进行错误操作,比如使用了 JSON.parse(data) 对数据进行转 ...

  8. mysql 分组条件筛选

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  9. Linux 磁盘格式化、检验、挂载

    分区完毕之后自然要进行文件系统的格式化.格式化命令mkfs(make file system)这个命令.这是个综合命令,它会去调用正确的文件系统格式化工具软件. 磁盘格式化 mkfs mke2fs m ...

  10. GWAS Catalog数据库简介

    GWAS Catalog The NHGRI-EBI Catalog of published genome-wide association studies EBI负责维护的一个收集已发表的GWAS ...