python第二章

字符串讲解;
#---------字符串各种方法----------
s = "Hello world!"
#1.swapcase():大小写互换
print(s.swapcase())
#输出:hELLO WORLD!
#2.capitalize():返回第一个字符为大写,其他字符为小写
#源码:
'''
Return a capitalized version of S, i.e. make the first character
        have upper case and the rest lower case.
'''
print(s.capitalize())
#输出:Hello world!
#3.casefold():返回所有字符为小写
#源码:
'''
Return a version of S suitable for caseless comparisons.
'''
print(s.casefold())
#输出:hello world!
#4.center():参数为一个数字和一个字符,在这个数字为长度的范围内,字符串居中,两边由那个字符补足
print(s.center(50,'#'))
#输出:###################Hello world!###################
#5.count():统计字符串中某个字符的个数,也可以从一个范围开始
#如:print(s.count('o',0,5))  输出为1
print(s.count('o'))
#输出:2
#6.endswith():判断字符串以什么结尾
print(s.endswith('d!'))
#输出:True
#7.expandtabs():扩展tab键
s2 = 'a\tb'    #\t就是tab键
print(s2.expandtabs())
#输出:a       b    #tab键默认8个空格
print(s2.expandtabs(20))   #tab键可以指定空格
#输出:a                   b
#8.find():返回字符在字符串中的位置,没有则返回-1;也可以从一个范围开始
#如:print(s.find('o',0,3))  输出:-1
print(s.find('o'))
#输出:4
print(s.find('sdaf'))
#输出:-1
#9.format():字符串格式化输出
#方法一:
s3 = 'my name is {0},i am {1} years old'
print(s3.format('Mm',18))
#输出:my name is Mm,i am 18 years old
#方法二:
s4 = 'my name is {name},i am {age} years old'
print(s4.format(name = 'Mm',age = 18))
#输出:my name is Mm,i am 18 years old
#10.isalnum():是否为阿拉伯数字或字符
print('22'.isalnum())
#输出:True
#11.isalpha():是否为字符
print('mm'.isalpha())
#输出:True
#12.isdecimal():是否是个数字
#13.isdigit():是否是个数字
#14.isidentifier():是否是个合法的变量名
#15.islower():是否都是小写
#16.isnumeric():是否都是数字
#17.isprintable():是否是可打印的(文本文件或者是字节型文件)
#18.issapce():判断是否是空格
print(' '.isspace())
#输出:True
#19.istitle():判断是不是标题
#20.isupper():判断是不是都是大写
#21.join():列表元素以一种方式,如下例中的'*'连接成字符串
names = ['mm','mumu','tt']
print('*'.join(names))
#输出:mm*mumu*tt
#22.ljust():右填充
print(s.ljust(50,'*'))
#输出:Hello world!**************************************
#23.rjust():左填充
print(s.rjust(50,'#'))
#输出:######################################Hello world!
#24.lower():将字符串改成小写
print(s.lower())
#输出:hello world!
#25.upper():将字符串改成大写
print(s.upper())
#输出:HELLO WORLD!
#26.strip():去掉空格或换行
#27.lstrip():只去掉左边空格或换行
#28.rstrip():只去掉右边空格或换行
#29.maketrans():生成对应表
#30.translate():翻译
str_in = 'abcdef'
str_out = '!@#$%^'
table = str.maketrans(str_in,str_out)
print(table)
#输出:{97: 33, 98: 64, 99: 35, 100: 36, 101: 37, 102: 94}
print(s.translate(table))
#输出:H%llo worl$!
#31.partition():一个字符串以其中的字符分割成三段
print(s.partition('o'))
#输出:('Hell', 'o', ' world!')
#32.rpartition():一个字符串以其中的字符分割成三段,从右边开始寻找
print(s.rpartition('o'))
#输出:('Hello w', 'o', 'rld!')
#33.replace():替换,不指定一个数字将全部换掉
print(s.replace('o','*',1))
#输出:Hell* world!
#34.rfind():从右边开始找,找不到的时候为-1
print(s.rfind('l'))
#输出:9
#34.rindex():从右边找,当找不到的时候报错
#35.split(): 默认以空格来分割字符串为列表
print(s.split())
#输出:['Hello', 'world!']
#36.rsplit():从右边开始分
#如:
print(s.split('o',1))
print(s.rsplit('o',1))
#输出:
# ['Hell', ' world!']
# ['Hello w', 'rld!']
#37.splitlines():按行来分
t = 'a\nb\nmm\nc'
print(t.splitlines())
#输出:['a', 'b', 'mm', 'c']
#38.startswith():判断以什么开始
print(s.startswith('He'))
#输出:True
#39.zfill():字符串变成一个长度的,不够用0填充
print(s.zfill(40))
#输出:0000000000000000000000000000Hello world!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值