python supper函数_第四节: 字符串魔法功能

1、capitalize 首字母大写

test = "alex"

#首字母大写

v = test.capitalize()

print(v)

结果:

Alex

2、大小写转换

2.1)大写字母转换成小写

lower

casefold(很多未知的都能变小写,一般用不到)

![](https://box.kancloud.cn/afcf3b92cb1e46c3da3dc35a2a6628b2_767x217.png)

3、center

功能:设置这个字符占的宽度

#def center(width,fillchar=None) 遇到=,可以带,可以不带

#设置宽度,并将内容居中

# width 20 代表总长度

# * 空白位置填充,只能带一个字符,也可以不填写

test = "ALEX"

v1 = test.center(20,"*")

print(v1)

类似:

ljust: 放左边

rjust:放右边

![](https://box.kancloud.cn/fbcb592edc4bf7838c818940ae7da183_766x129.png)

4、count

功能: 判断这个字母出现的次数

#def count(self, sub, start=None, end=None)

#start 表示从第几位开始

#end 表示找到第几位结束

#默认是所有的,从第一位开始找

test = "louisalexfix"

v1 = test.count('l')

print(v1)

test = "loexuiexlexfix"

v1 = test.count('ex',5,10)

print(v1)

4、endswith

功能:以()里的结尾, 可以用于条件判断

![](https://box.kancloud.cn/780536051fdc5aa94016c91fd5082ca4_999x448.png)

5、startswith

功能: 以()里的开头,可以用于条件判断

![](https://box.kancloud.cn/19655a33bf470cdc33a25438efa2d248_894x422.png)

6、find

功能: 从前往后查找,找到第一个,获取其位置,之后的就不查找咯。不能找到显示-1

test = "alexalexfiex"

v1 = test.find('ex')

print(v1)

结果: 2

![](https://box.kancloud.cn/001c6fb2d804f6ddca2861de04200c55_776x218.png)

7、format

功能: 格式化,将字符串上的占位符,替换成指定的值

![](https://box.kancloud.cn/362f1b8d6651b9b52f68924293d5509a_785x120.png)

![](https://box.kancloud.cn/39d8a9e79794102e53bb46dcc4647508_780x114.png)

![](https://box.kancloud.cn/bb039d9292973777210bfffea6ce87c9_525x114.png)--format_map

name = input("please input name:")

age = input("please input age: ")

tem="I am {0},age {1}"

v1 = tem.format(name,age)

print(v1)

8:判断是否是啥 ---可以用于条件判断

8.1)isalum

字符串里只能出现字母和数字

![](https://box.kancloud.cn/c7d2665a33f2700fdd5771107d43b6d4_946x218.png)

8.2) isalpha

判断是否为字母、汉子

test = "我"

v1 = test.isalpha()

print(v1)

![](https://box.kancloud.cn/1be35e66092c5d7e7175e4af3bceff16_846x220.png)

8.3) isdigit

功能:判断当前是不是数字

![](https://box.kancloud.cn/d2b924ecb4ee7956c5a5ce37c2667f07_863x128.png)

相似的:

isnumeric 支持中文的数字,比如二

isdecimal 支持特殊的字符2

案例:

![](https://box.kancloud.cn/8ea46408c73c3b111b920b22239b9c6d_848x296.png)

注意: 以后用的最多的,还是isdecimal比较多

8.4) isidentifier

功能:判断是否有一个标识符,需要满足有字母,数字,下划线

以数字开头的,都为false

![](https://box.kancloud.cn/900c2b6b4dd9a334f9e228327adf3038_1008x344.png)

![](https://box.kancloud.cn/ce43103c1fe782746c358a713c9aecf2_817x337.png)

8.5) isspace

功能:是否为空格

![](https://box.kancloud.cn/515e63549420a69bb9620a9f2ad4e835_781x321.png)

8.6) islower lower

功能: 判断是否全部为小写字母和转换为小写

![](https://box.kancloud.cn/de9c7e4182cd6d9429f175e3d34a603a_413x111.png)

8.7) isupper supper

功能:判断是否全部为大写,和转换给大写

![](https://box.kancloud.cn/4a2391ea566b91752d5046c2bcac6516_522x119.png)

9: expandtabs

功能:分隔,对齐 expandtabs(n),以n个为一组,如果遇到\t, 就以空格补全

test="abcdeffd\tdif\tdi\t"

v1 = test.expandtabs(6)

print(v1)

原理: abcdef fd\t dif\t di\t

遇到\t,把不够的用空格标识,例如这里是6,然后fd只占有2个,遇到\t咯,那么就需要补4个空格,依次类推

![](https://box.kancloud.cn/5cf2584f45b318f6f967c994074a8202_945x155.png)

10 join -------非常重要

功能:#将字符串中的每一个元素按照指定的分隔符分开

test = "你是风儿我是沙"

v1 = "_".join(test)

print(v1)

11、lstrip rstrip strip

功能:

1)去掉空白

2)去掉\n,\t特殊的字符

3)去掉指定的字符,以先最多匹配去除,依次去找

lstrip: 取出左边的

rstrip: 取出右边的

strip: 取出左右两边

![](https://box.kancloud.cn/d9b970421427c2b13d56b227f93a87d7_741x476.png)

![](https://box.kancloud.cn/b9949c991975ff3f1e7404df823a2e91_793x203.png)

12、partition

功能: 以某个()东西为分隔,只能分隔成三分

split:

匹配到的字符拿不到

可以指定分隔的个数

默认是全部

![](https://box.kancloud.cn/0112d3f1b26d07aff6a22ee974b08767_833x487.png)

13、字符串替换replace

![](https://box.kancloud.cn/c9af9a256b39609108ac97d6cb8fb5bf_828x308.png)

重点:

join 连接

split(split,lspit,rspit)分隔

find (查找)

strip(lstrip,rstrip,strip)去除

upper 大写

lower 小写

replace 替换

#########################灰魔法#######################

1、获取字符串里的某一个字符,索引从0开始

![](https://box.kancloud.cn/b994182c769c205dfa4b3efff7833612_663x212.png)

2、切片, 左边 <=x < 右边

![](https://box.kancloud.cn/4011afb0bc09b4437a0ee60789275e77_832x385.png)

3、len

test = "我来自云南省昆明市"

index = 0

while index < len(test):

t = test[index]

print(t)

index += 1

test = "我来自云南省昆明市"

#

for index in test:

print(index)

4、循环(for,while)

test = "thisidfswork"

v1 = test.split('s')

for i in v1:

print(i)

![](https://box.kancloud.cn/96e7b5bef4c75b090c96520d576fc8f5_802x155.png)

5、range

功能:帮助创建连续的数字,默认从0开始,也可以创建不连续的数字

在python2中会直接创建,在python3中,要用循环,才会打印出来

![](https://box.kancloud.cn/017730c36f6154bf391944a3aa92c307_746x225.png)

![](https://box.kancloud.cn/4067ad7f3cd3798dfbe5523247de4568_919x380.png)

test = input("please input: ")

len = len(test)

for item in range(0,len):

print(item,test[item])

案例:打印一个表格

test = " "

while True:

t = input("please input caozhuo: ")

if t == "y":

name = input("please input name: ")

pwd = input("please input passwd: ")

email = input("please input email adddress: ")

temp = "{0}\t{1}\t{2}\n"

v1 = temp.format(name,pwd,email)

test = test + v1

elif t == "q":

break

else:

pass

print(test.expandtabs(20))

![](https://box.kancloud.cn/4f2872fa8d6a09f046f66487c0ae9770_796x432.png)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值