1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
| path = r'C:\windows\python.exe' ```python
```python print('%d %s' % (a, b)) ```python
‘-’ 指定左对齐 ‘+’ 正数前面加+号 ‘.n’ 指定精度
```python >>>'%.2f' % 0。33333 0.33 ```python
和C++基本相同,
* %d 十进制整数 * %e 以e为底的指数 * %o 八进制整数
基本语法是通过’{}’,’:‘替换之前的’%’
```python >>>'{:%}'.format(3.5) '350.0000%' ```python
[各类格式字符含义](https://www.runoob.com/python/att-string-format.html)
```python "{}{}".format('Hello', 'World') >>> "{0}{1}{0}".format('Hello', 'World'') # 指定位置 'World Hello World’ >>> print("my name is {name}, my age is {age}, and my QQ is {qq}".format(name = "Dong Fuguo",age = 40,qq = "30646****")) my name is Dong Fuguo, my age is 40, and my QQ is 30646**** ```python
与format方法基本类似
```python >>> name = 'Dong' >>> age = 39 >>> f'My name is {name}, and I am {age} years old.' 'My name is Dong, and I am 39 years old.'
>>> width = 10 >>> precision = 4 >>> value = 11/3 >>> f'result:{value:{width}.{precision}}' 'result: 3.667' ```python
```python find(str,beg=0,end=len(str)) rfind(str,beg=0,end=len(str))) index(str,beg=0,end=len(str))) rindex(str,beg=0,end=len(str))) count() ```python
* find()和rfind方法分别用来查找一个字符串在另一个字符串指定范围(默认是整个字符串)中首次和最后一次出现的位置,如果不存在则返回-1 * index()和rindex()方法用来返回一个字符串在另一个字符串指定范围中首次和最后一次出现的位置,如果不存在则**抛出异常** * count()方法用来返回一个字符串在当前字符串中出现的次数
```python split(str="", num=string.count(str))、rsplit(str="",num=string.count(str)) ```python
* split()和rsplit()方法分别用来以指定字符为分隔符,把当前字符串从左往右或从右往左分隔成多个字符串,并返回包含分隔结果的列表 * partition()和rpartition()用来以指定字符串为分隔符将原字符串**分隔为3部分**,即**分隔符前的字符串、分隔符字符串、分隔符后的字符串**,如果指定的分隔符不在原字符串中,则返回**原字符串和两个空字符串**。把字符串 string 分 成 一 个 3 元 素 的 元组(string\_pre\_str,str,string\_post\_str)
返回通过指定字符连接序列中元素后生成的新字符串。
```python >>> li = ["apple","peach","banana","pear"] >>> ','.join(li) 'apple,peach,banana,pear' ```python
```python >>> s = "What is Your Name?" >>> s.lower() 'what is your name?' >>> s.upper() 'WHAT IS YOUR NAME?' >>> s.capitalize() >>> s.title() >>> s.swapcase() 'wHAT IS yOUR nAME?' ```python
查找替换replace(old, new[, max]),类似于Word中的“全部替换”功能
* old – 将被替换的子字符串 * new – 新字符串,用于替换old子字符串 * max – 可选字符串, 替换不超过 max 次
注意:返回的是一个新字符串
```python >>> s ="中国,中国" >>> print(s) 中国,中国 >>> s2 = s.replace("中国","中华人民共和国") >>> print(s2) 中华人民共和国,中华人民共和国 ```python
字符串对象的maketrans()方法用来生成字符映射表,而translate()方法用来根据映射表中定义的对应关系转换字符串并替换其中的字符,使用这两个方法的组合可以同时处理多个字符。
```python
>>> table = ''.maketrans('abcdef123','uvwxyz@#$') >>> s ="Python is a greate programming language. I like it!"
>>> s.translate(table) 'Python is u gryuty progrumming lunguugy. I liky it!' ```python
一些常用的全体字母,并实现凯撒加密
```python lower = string.ascii_lowercase upper = string.ascii_uppercase before = string.ascii_letters after = lower[k:] + lower[:k] + upper[k:] + upper[:k] table =''.maketrans(before, after) return s.translate(table) ```python
```python >>> s =" abc " >>> s.strip() 'abc' >>> '\n\nhello world \n\n'.strip() 'hello world' >>> "aaaassddf".strip("a") 'ssddf' >>> "aaaassddf".strip("af") 'ssdd' >>> "aaaassddfaaa".rstrip("a") 'aaaassddf' >>> "aaaassddfaaa".lstrip("a") 'ssddfaaa' ```python
注意,这三个函数的参数指定的字符串并不作为一个整体对待,而是在原字符串的两侧、右侧、左侧删除参数字符串中包含的所有字符
```python >>> 'aabbccddeeeffg'.strip('gbaefcd') '' ```python
s.startswith(t, beg=0,end=len(string))、s.endswith(t,beg=0,end=len(string)),判断字符串是否以指定字符串开始或结束
```python >>> s = 'Beautiful is better than ugly.' >>> s.startswith('Be') True >>> s.startswith('Be', 5) False >>> s.startswith('Be', 0, 5) True ```python
isalnum()、isalpha()、isdigit()、isspace()、isupper()、islower(),用来测试字符串是否为数字或字母、是否为字母、是否为数字字符、是否为空白字符、是否为大写字母以及是否为小写字母。
```python >>> '1234abcd'.isalnum() True >>> '1234abcd'.isalpha() False >>> '1234abcd'.isdigit() False >>> 'abcd'.isalpha() True >>> '1234.0'.isdigit() False ```python
center()、ljust()、rjust(),返回指定宽度的新字符串,原字符串居中、左对齐或右对齐出现在新字符串中,如果指定宽度大于字符串长度,则使用指定的字符(默认为空格)进行填充。
```python >>> 'Hello world!'.center(20) ' Hello world! ' >>> 'Hello world!'.center(20,'=') '====Hello world!====' >>> 'Hello world!'.ljust(20,'=') 'Hello world!========' >>> 'Hello world!'.rjust(20,'=') '========Hello world!' ```python
生成新的字符串
```python >>> 'hello ' + 'world' 'hello world' ```python
成员判断,关键字in
```python >>> "a" in "abcde" True >>> 'ab' in 'abcde' True >>> 'ac' in 'abcde' False >>> "j" in "abcde" False ```python
Python字符串支持与整数的乘法运算,表示序列重复,也就是字符串内容的重复,得到新字符串
```python >>> 'abcd' * 3 'abcdabcdabcd' ```python
```python >>> x = 'Hello world.' >>> len(x) 12 >>> max(x) 'w' >>> min(x) ' ' >>> list(zip(x,x)) [('H','H'), ('e','e'), ('l','l'), ('l','l'), ('o','o'), (' ','',('w','w'), ('o','o'), ('r','r'), ('l','l'), ('d','d'), ('.','.')] >>> sorted(x) [' ','.','H','d','e','l','l','l','o','o','r','w'] >>> list(reversed(x)) ['.','d','l','r','o','w',' ','o','l','l','e','H'] >>> list(enumerate(x)) [(0,'H'), (1,'e'), (2,'l'), (3,'l'), (4,'o'), (5,' '), (6,'w'),(7,'o'), (8,'r'), (9,'l'), (10,'d'), (11,'.')] >>> list(map(add, x, x)) ['HH','ee','ll','ll','oo',' ','ww','oo','rr','ll','dd','..']
|