Python的字符串和字符操作

Python的字符和字符串操作

# -*- coding:utf-8 -*-
# Author:Starhao

name = "{name}ahao"

print(name.capitalize())#首字母大写
print(name.count("a"))#统计字符个数
print(name.center(50,"-"))#规定字符串长度字符不够不全,字符串放中间可用于美化输出
print(name.endswith("o"))#判断字符串以什么结尾
print(name.expandtabs(tabsize=3))#把\t转化为空格
print(name.find('name'))#查找字符的位置可以用于切片
print(name.format(name="大王"))#格式化输出
print(name.format_map({'name':"大王"}))#格式化输出
print('dshjd12'.isalnum())#判断是不是英文字母或者数字
print('dshjd12'.isalpha())#判断是不是英文字符
print('12A'.isdecimal())#是否为十进制
print('dshjd12'.isdigit())#是不是整数
print('dshjd12'.isidentifier())#判断是不是一个合法的变量名
print('dAhjd12'.islower())#判断是不是小写
print('dshjd12'.isnumeric())#判断是不是数字不能有标点和浮点
print('Dshjd12'.istitle())#是不是每个首字母大写
print('dshjd12'.isprintable())#是否能打印
print('dshjd12'.isupper())#是不是都为大写字母
print('+'.join(name))#看输出结果输出结果很容易理解
print(name.ljust(50,'%'))#长度不够用其在后边补够
print(name.rjust(50,'#'))#长度不够在其前边补全
print('GDAHJ'.lower())#大写变小写
print('dshjd12'.upper())#小写变大写
print('\n\ndshjd12'.lstrip())#去掉字符串左边的空格和回车
print('dshjd12\n'.rstrip())#去掉字符串右边的空格和回车
print('      dshjd12\n'.strip())#去掉字符串的空格和回车
p = str.maketrans('abcdef','123456')#将前边的进行转化为后边
print('ahao li'.translate(p))#将p的值创给前边字符串
print("fdsdfs".replace('d','D',1))#替换
print("fdsdfs".rfind('d'))#找到最右边的d的位置
print("1+2+3+4".split('+'))#将其转化为列表
print("fds\ndfs".splitlines())#按换行将其转化为列表
print("fdsdfs".swapcase())#大写转小写,小写转大写
print("fdsdfs".title())#首字母大写
print("fdsdfs".zfill(10))#长度不够用0补全

猜你喜欢

转载自blog.csdn.net/weixin_43222122/article/details/88875747