Python学习【day02】-str类型方法记录
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 4 #测试并记录str数据类型的各个方法 5 6 str1 = "root" 7 #收字母大写 8 v1 = str1.capitalize() 9 print(v1) 10 11 str2 = "RooT" 12 #字符串转为小写 推荐使用 会将各国的字符都变为小写 13 v2 = str2.casefold() 14 print(v2) 15 #只可将常规的英文大写字符变为小写 16 v2 = str2.lower() 17 print(v2) 18 19 #转换后的字符串占多少位,且原字符串在中间,左右以 * 填充 也可不写,若不写 默认为空格 20 # 若设置位数小于源字符串位数,则显示原字符串 21 #若位数无法平分,多出的填充字符放置在原字符串右边 22 v3 = str2.center(10, "*") 23 print(v3) 24 25 str3 = "pythonpy" 26 #在当前字符串中查询子序列出现的次数,可指定在起止位置查找出现的次数 27 v3 = str3.count("py") 28 print(v3) 29 v3 = str3.count("py", 0, 4) 30 print(v3) 31 32 #是否为某个字符串结尾,可指定某一段字符串 33 v3 = str3.endswith("py") 34 print(v3) 35 v3 = str3.endswith("h", 0, 4) 36 print(v3) 37 38 #将#TAB替换为几个空格 包含#TAB前面的字符 39 str4 = "aaaaa\t9" 40 print(str4) 41 v4 = str4.expandtabs(20) 42 print(v4) 43 44 #找字符串首次出现的位置,若没有 则返回-1, 同时可以指定在固定位置查找 推荐使用 45 str5 = "pythonpy" 46 v5 = str5.find("th") 47 print(v5) 48 v5 = str5.find("the") 49 print(v5) 50 51 v5 = str5.find("py", 3) 52 print(v5) 53 54 #字符串格式化 55 str6 = "i am {name}, age {age}" 56 print(str6) 57 v6 = str6.format(name="Jack", age=18) 58 print(v6) 59 str6 = "i am {0}, age {1}" 60 v6 = str6.format("Lucy", 16) 61 print(v6) 62 #以map格式的字符串格式化 63 str6 = "i am {name}, age {age}" 64 v6 = str6.format_map({"name":"Tom","age":20}) 65 print(v6) 66 67 68 """ 69 首次出现的位置,若没有则报错,同时可以指定在固定字符串范围内查找 和 find比较 推荐使用find 70 Traceback (most recent call last): 71 File "D:/Python_Study/day02/str.py", line 69, in <module> 72 v7 = str7.index("one") 73 ValueError: substring not found 74 """ 75 str7 = "pythonpy" 76 v7 = str7.index("py") 77 print(v7) 78 v7 = str7.index("py", 3) 79 print(v7) 80 81 #字符串是否只包含字符和数字,若是则返回True 否则False 82 str7 = "abc123" 83 v7 = str7.isalnum() 84 print(v7) 85 str7 = "abc123_" 86 v7 = str7.isalnum() 87 print(v7) 88 89 #字符串是否只包含字符,若是则返回True 否则False 90 v7 = str7.isalpha() 91 print(v7) 92 str7 = "abc" 93 v7 = str7.isalpha() 94 print(v7) 95 96 #字符串是否是ascii码 是返回True 否则False 97 v7 = str7.isascii() 98 print(v7) 99 str7 = "中国" 100 v7 = str7.isascii() 101 print(v7) 102 103 #字符串是一个十进制的整数 返回True 否则返回False 104 str8 = "101" 105 v8 = str8.isdecimal() 106 print(v8) 107 108 #字符串是一个数字返回True 否则返回False 109 str9 = "01010" 110 v9 = str9.isdigit() 111 print(v9) 112 113 #字符串是否是python的有效命名方式 是返回True 否则返回False 114 str10 = "sum_10" 115 v10 = str10.isidentifier() 116 print(v10) 117 str10 = "10_sum" 118 v10 = str10.isidentifier() 119 print(v10) 120 121 #是否是数字 是True 否False 122 str11 = "11" 123 v11 = str11.isnumeric() 124 print(v11) 125 126 #是否可打印 127 str12 = "" 128 v12 = str12.isprintable() 129 print(v12) 130 131 #字符串是否为空格 包含转义空格 #TAB等 132 str13 = "\t" 133 v13 = str13.isspace() 134 print(v13) 135 136 #是否可为标题 各个单词首字母大写 是True 否False 137 str14 = "Is Is" 138 v14 = str14.istitle() 139 print(v14) 140 str14 = "Is is" 141 v14 = str14.istitle() 142 print(v14) 143 144 #字符串英文字符是否为大写 145 str15 = "A123中" 146 v15 = str15.isupper() 147 print(v15) 148 149 #以当前字符串为连接符将join中的字符串连接起来 150 str16 = "python" 151 v16 = str16.join(["A", "B", "C", "D", "Ee"]) 152 print(v16) 153 154 #左对齐 以设置的字符填充到设置长度的字符串 155 str17 = "python" 156 v17 = str17.ljust(15, "P") 157 print(v17) 158 159 #去除字符串左边的空格 160 str18 = " p ython " 161 v18 = str18.lstrip() 162 print(v18) 163 164 # str19 = "python" 165 # v19 = str19.maketrans() 166 # print(v19)