python 字符串操作

#字符串方法 str
t1 = "michael"
print(t1.capitalize()) #首字母大写
t2 = "MicHael"
#所有变小写,casefold 更强,可以把很多未知的对应的变小写
print(t2.casefold())
print(t2.lower())

t3 = t1
print(t3.center(20,"=")) #设置宽度,并将内容居中 20 代表总长度,= 表示填充位置字符,默认是空白,只能填一个字符
print(t3.ljust(10,'*')) #左填充
print(t3.rjust(10,"-"))#右填充


#去找字符中子序列出现的次数
t4 = "michaelmichaelmichael"
t4.count("mi",5,10)# 5 和10 表示是从第几个字符开始查找到第几个字符结束

#encode

#decode


# 以什么什么结尾的
#以什么什么开始的
t5 ="michael"
print(t5.endswith("ae",1,2))
print(t5.startswith("e"))

t6="username\tpassword\tEmail\nhan\t123123\[email protected]\nhan\t123123\[email protected]\nhan\t123123\[email protected]\nhan\t123123\[email protected]"
print(t6.expandtabs(10))


#格式化,将一个字符串中的占位符替换为指定值,这地方注意大括号
# t7 = 'I,am {name},gae {a}'
# print(t7)
# v7 = t7.format(name='alex',a=19)
# print(v7)

#占位符的另一种方式
# t7 = 'I,am {0},gae {1}'
# print(t7)
# v7 = t7.format('alex',19)
# print(v7)


# #格式化传入的值{"name":'han',"a":19} 字典格式 注意大括号
# t8 = 'I,am {name},gae {a}'
# print(t8)
# v8 = t8.format_map({"name":"han","a":19})
# print(v8)


#去查找索引值,index,如果找不到会报错,但是find 不会 注意区别
#find index也有起始位置 和结束位置
# t9 = "michael"
# print(t9.find("ch"))
# print(t9.index("ae"))



#判断字符串中是否只包含 数字和字母
# t10 ="michael _+"
# print(t10.isalnum())
# t11="michael"
# print(t11.isalpha()) #是否是字母 汉字

#判断是不是 数字
# t12 = "123324"
# v12 = t12.isdecimal()#
# v13 = t12.isdigit()


t13 = "ldsag"
#判断都是小写 ,把所有变成小写
v13 = t13.islower()
v14 = t13.lower()
print(v13,v14)
#判断是否都是大写,下面把所有变成大写
v15 = t13.isupper()
v16 = t13.upper()
print(v15,v16)

#去除左或右的空格,处理左右空格,默认去除空白
# t14 = " alskgja "
# t14.lstrip()
# t14.rstrip()
# t14.strip()
#移除 \t \n 指定字符,最低匹配
# t14 = "#alskgja "
# t14.lstrip("x") #去掉从左指定的字符
# t14.rstrip("gj") #去掉从右指定字符
# t14.strip()

未完待续。。。

猜你喜欢

转载自www.cnblogs.com/michael2018/p/8990626.html