Python学习之字符串11

Python字符串及其内置方法


乘法操作

print('hello'*3)

hellohellohello

切片操作

print('hello'[2:])

llo

字符串判断

print('e21' in 'hello')

False

字符串拼接

#方法一
a = '123'
b = 'abc'
c = a + b
print(c)

123abc
#方法二 拼接 效率比一高
c = ''.join([a,b])  #以''拼接
c = '----'.join([a,b])  #以'----'拼接

123abc
123----abc

内置方法

st = 'hello kitty'

st.count('t')  #统计t的个数  重要

st.capitalize() #将首字母大写

st.center(50,'-') #打印50个字符,其他用-补充,st居中 

st.encode()
st.decode()

st.endwith('y')   #返回boolean 
st.startswith('h') #返回boolean  重要

st.expandtabs(tabsize = 10)  #设置一个tab多少个空格

st.find('t')  #查找到第一个元素,并将索引值返回  重要

st = 'hello kitty {name}'
st.format(name = 'alex') #格式化输出  重要
st.format_map({'name':'alex'})

st.index('t')   #与find一样

st.isalnum()  #是否包含数字或字母

st.isdecimal() #判断是否是十进制的数

st.isdigit()  #判断是不是一个数字  重要
'126.9999'.isdigit() #false 必须是一个整型

st.isnumberic() #与isdigit()一样

st.isidenetifier()  #判别是不是非法变量

st.islower()  #判断全部是否是小写
st.isuper()   #大写

st.isspace()  #是否是空格

st.istitle()  #判断首字母大写 这个自己再试一下

st.lower()  #所有大写变小写 重要
st.uper()  #小写变大写  重要
st.swapcase #大小写互换

st.ljust(50,'*')  #左对齐 考虑center 
st.rjust(50,'*')   #右对齐

st.strip()  #tab,空格,换行符去掉  特别重要

st.replace('hello','hi') #将hello替换成hi  重要

st.rfind('t')  # 从右往左返回t的位置

st.split(' ') #以' '分割st的得到一个列表  重要
st.rsplit(' ') #以右为准

st.title()  #以title格式重写st

猜你喜欢

转载自blog.csdn.net/The_last_knight/article/details/83239178