Update 08. 字符串.md

This commit is contained in:
LSGOMYP
2020-07-22 17:29:33 +08:00
parent 9b40774bd0
commit 90d93211ad

View File

@@ -299,14 +299,16 @@ print(str6.splitlines(True)) # ['I \n', ' Love \n', ' LsgoGroup']
- `maketrans(intab, outtab)` 创建字符映射的转换表,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
- `translate(table, deletechars="")` 根据参数`table`给出的表,转换字符串的字符,要过滤掉的字符放到`deletechars`参数中。
【例子】
```python
str = 'this is string example....wow!!!'
str7 = 'this is string example....wow!!!'
intab = 'aeiou'
outtab = '12345'
trantab = str.maketrans(intab, outtab)
trantab = str7.maketrans(intab, outtab)
print(trantab) # {97: 49, 111: 52, 117: 53, 101: 50, 105: 51}
print(str.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!
print(str7.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!
```
@@ -318,17 +320,17 @@ print(str.translate(trantab)) # th3s 3s str3ng 2x1mpl2....w4w!!!
【例子】
```python
str = "{0} Love {1}".format('I', 'Lsgogroup') # 位置参数
print(str) # I Love Lsgogroup
str8 = "{0} Love {1}".format('I', 'Lsgogroup') # 位置参数
print(str8) # I Love Lsgogroup
str = "{a} Love {b}".format(a='I', b='Lsgogroup') # 关键字参数
print(str) # I Love Lsgogroup
str8 = "{a} Love {b}".format(a='I', b='Lsgogroup') # 关键字参数
print(str8) # I Love Lsgogroup
str = "{0} Love {b}".format('I', b='Lsgogroup') # 位置参数要在关键字参数之前
print(str) # I Love Lsgogroup
str8 = "{0} Love {b}".format('I', b='Lsgogroup') # 位置参数要在关键字参数之前
print(str8) # I Love Lsgogroup
str = '{0:.2f}{1}'.format(27.658, 'GB') # 保留小数点后两位
print(str) # 27.66GB
str8 = '{0:.2f}{1}'.format(27.658, 'GB') # 保留小数点后两位
print(str8) # 27.66GB
```