Python全栈之路:string字符串常用方法
2017-12-23 18:35
501 查看
大小写转换
print("name is Alex".capitalize()) # 转为首字母大写 ->Name is alex print("name is Alex".title()) # 转为标题形式 ->Name Is Alex print("Name".upper()) # 转大写 ->NAME print("Name".lower()) # 转小写 ->name print("Name".swapcase()) # 交换大小写 ->aLEX
字符串判断
print("name is Alex".startswith("name")) # 以...开头 ->True print("name is Alex".endswith("Alex")) # 以...结尾 ->True print("123abc".isalnum()) # 字母或数字 ->True print("abc".isalpha()) # 字母 ->True print("123".isdigit()) # 数字 ->True print("123".isnumeric()) # 数字 ->True print("abc".isidentifier()) # 判断是否为合法变量名 ->True print(" ".isspace()) # 空格 ->True print("My Name Is Tom".istitle()) # 标题 ->True print("My Name Is Tom".isprintable()) # 可打印 tty file, drive file ->True print("MY".isupper()) # 大写 ->True print("my".islower()) # 小写 ->True
输出显示
print(",".join(["1", "2", "3"])) # 连接 # ->1,2,3 print("name is Alex".split()) # 拆分为列表,默认拆分符为空格 # ->['name', 'is', 'Alex'] print("name\n is\r\n Alex".splitlines()) # 拆分为列表,默认拆分符为回车 # ->['name', ' is', ' Alex'] print("name".center(50, "-")) # 内容居中,两侧填充 # ->-----------------------name----------------------- print("name".ljust(50, "*")) # 内容靠左,以*填充 # ->name********************************************** print("name".rjust(50, "-")) # 内容靠右,以-填充 # ->----------------------------------------------name print("name".zfill(50)) # 内容靠右,以0填充 # ->0000000000000000000000000000000000000000000000name print("name is\t Alex".expandtabs(tabsize=20)) # 设置'\t'宽度 # ->name is Alex print("I am from {city},and {year} years old".format(city="Wuhan", year=23)) # 格式化 # ->I am from Wuhan,and 23 years old print("I am from {city},and {year} years old".format_map({"city": "Wuhan", "year": 23})) # 字典参数格式化 # ->I am from Wuhan,and 23 years old print("\nname".lstrip()) # 去除左侧回车和空格 ->name print("name\n".rstrip()) # 去除右侧回车和空格 ->name print(" name\n".strip()) # 去除两侧回车和空格 ->name print("---")
查找替换
p = str.maketrans("abc", "123") # 创造翻译字典 print(type(p)) # -><class 'dict'> print("abc".translate(p)) # 翻译,参数:dict ->123 print("name is Tom".replace("n", "N")) # 替换 ->Name is Tom print("name is Alix".count("i")) # 字符计数 ->2 print("name is Alex".find("e")) # 查找,返回下标,没有则返回-1 ->3 print("name is Alex".rfind("e")) # 右边开始查找 ->10 print("name is Alex".index("e")) # 返回索引,不存在则报错 ->3 print("name is Alex".rindex("e")) # 右边开始查找 ->10
相关文章推荐
- Python全栈之路:集合set常用方法
- Python全栈之路:list列表常用方法
- Python 字符串常用方法 string
- 5.7-全栈Java笔记:字符串String的常用方法
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- 【Objective-C】OC中可变字符串(NSMutableString)常用方法
- String字符串的常用方法
- python 字符串,列表,字典常用方法
- 收集常用的Python 内置的各种字符串处理 函数的使用方法
- string字符串常用处理方法
- java中String的常用方法 1、length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars);
- Python - 翻转(reverse)字符串(string)的所有方法 详解
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python 字符串常用函数方法整合
- 转--Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python 判断字符串中字符类型的常用方法
- python 字符串常用方法总结
- python中字符串的常用方法
- 跟我一起学Python之九:字符串常用方法