python This is all you need to master strings
- 2021-12-12 09:07:28
- OfStack
4 major prefixes
In addition to ordinary strings, python can have four prefixes before strings, namely frub. Where the f string sets the
{}
Convert variables in to strings;
r
Indicates the cancellation of escape;
u
Indicates the use of Unicode characters;
b
Indicates the adoption of
byte
Type.
The first two are most commonly used, and examples are as follows
>>> f"1+1={1+1}" #f String
'1+1=2'
>>> r"C:\abc\def" #r String unescape
'C:\\abc\\def'
Case conversion
大小写转化 | 说明 | 示例 | 结果 |
---|---|---|---|
capitalize() | 首字母转大写 | CODE_TAG_REPLACE_MARK_5 | ‘I love u' |
upper() | 所有字母转大写 | CODE_TAG_REPLACE_MARK_6 | ‘I LOVE U' |
lower() | 所有字母转小写 | CODE_TAG_REPLACE_MARK_7 | ‘i love u' |
title() | 单词首字母大写 | CODE_TAG_REPLACE_MARK_8 | ‘I Love U' |
swapcase() | 大小写翻转 | CODE_TAG_REPLACE_MARK_9 | ‘i lOVE u' |
casefold() | 超级大写转小写 | CODE_TAG_REPLACE_MARK_10 | ‘γ' |
Space adjustment function
Where the input
r
1
Represents adjusting the character length to
r
1
If not specified, the rest of the positions are added as spaces.
空格调整 | 说明 | 示例 | 结果 |
---|---|---|---|
center(w) | 调整后原字符串居中 | CODE_TAG_REPLACE_MARK_13 | CODE_TAG_REPLACE_MARK_14 |
ljust(w) | 调整后原字符串居左 | CODE_TAG_REPLACE_MARK_15 | CODE_TAG_REPLACE_MARK_16 |
rjust(w) | 调整后原字符串居右 | CODE_TAG_REPLACE_MARK_17 | CODE_TAG_REPLACE_MARK_18 |
zfill(w) | 在左侧补0 | CODE_TAG_REPLACE_MARK_19 | ‘000123' |
expandtabs(w) | 将 CODE_TAG_REPLACE_MARK_20 转为w个空格 | CODE_TAG_REPLACE_MARK_21 | ' a' |
Delete some characters
u
2
Is a string, and if it is empty, it defaults to a space.
lstrip(s) | 自左删除 CODE_TAG_REPLACE_MARK_22 中的字符 | CODE_TAG_REPLACE_MARK_24 | ‘oveU' |
rstrip(s) | 自右删除 CODE_TAG_REPLACE_MARK_22 中的字符 | CODE_TAG_REPLACE_MARK_26 | ‘\tLove' |
strip(s) | 执行lstrip(s)和rstrip(s) | CODE_TAG_REPLACE_MARK_27 | ‘Love' |
removeprefix(s) | 自左删除 CODE_TAG_REPLACE_MARK_22 | CODE_TAG_REPLACE_MARK_29 | ‘ILoveU' |
removesuffix(s) | 自右删除 CODE_TAG_REPLACE_MARK_22 | CODE_TAG_REPLACE_MARK_31 | ‘ILov' |
String determination
返回True的条件 | 示例 | 返回值 | |
---|---|---|---|
isalnum() | 只包含字母或数字 | CODE_TAG_REPLACE_MARK_32 | True |
isalpha() | 只含字母 | CODE_TAG_REPLACE_MARK_33 | False |
isdecimal() | 只包含10进制数字 | CODE_TAG_REPLACE_MARK_34 | False |
isdigit() | 只含数字 | CODE_TAG_REPLACE_MARK_34 | False |
islower() | 包含字母,且均为小写 | CODE_TAG_REPLACE_MARK_36 | True |
isupper() | 包含字母,且均为大写 | CODE_TAG_REPLACE_MARK_37 | False |
isnumeric() | 只包含数字字符 | CODE_TAG_REPLACE_MARK_38 | True |
isspace() | 只含空格 | CODE_TAG_REPLACE_MARK_39 | False |
istitle() | 首字母均大写 | CODE_TAG_REPLACE_MARK_40 | True |
isascii() | 均为ASCII码 | CODE_TAG_REPLACE_MARK_41 | False |
isidentifier() | 可用作python标识符 | CODE_TAG_REPLACE_MARK_42 | False |
isprintable() | 均为可打印字符 | CODE_TAG_REPLACE_MARK_43 | False |
String lookup
The following functions all take three input parameters,
str
Represents the string to be matched,
beg
And
end
Represents the beginning and end, respectively, and defaults to 0 and the length of the matched string. With
s.count(str, beg, end)
For example, represent the
s[beg:end]
Medium
str
The number of appearances.
CODE_TAG_REPLACE_MARK_47 | CODE_TAG_REPLACE_MARK_44 出现的次数 |
CODE_TAG_REPLACE_MARK_52 | CODE_TAG_REPLACE_MARK_44 首次出现的位置,如未找到则返回-1 |
CODE_TAG_REPLACE_MARK_54 | CODE_TAG_REPLACE_MARK_44 最后出现的位置,如未找到则返回-1 |
CODE_TAG_REPLACE_MARK_56 | 和 CODE_TAG_REPLACE_MARK_57 相同,但未找到会报错 |
CODE_TAG_REPLACE_MARK_58 | 和 CODE_TAG_REPLACE_MARK_59 相同,但未找到会报错 |
CODE_TAG_REPLACE_MARK_60 | 若以 CODE_TAG_REPLACE_MARK_44 开头,则返回True |
CODE_TAG_REPLACE_MARK_62 | 若以 CODE_TAG_REPLACE_MARK_44 结尾,则返回True |
Split, merge and replace
CODE_TAG_REPLACE_MARK_64 | 将 CODE_TAG_REPLACE_MARK_22 按照 CODE_TAG_REPLACE_MARK_44 进行分割,若指定num,则分割为 CODE_TAG_REPLACE_MARK_67 段 |
CODE_TAG_REPLACE_MARK_68 | 和split相同,但从右向左开始匹配 |
CODE_TAG_REPLACE_MARK_69 | 按行分割,若指定keepends为False,则不保留换行符 |
CODE_TAG_REPLACE_MARK_70 | 以 CODE_TAG_REPLACE_MARK_22 为分隔符,将 CODE_TAG_REPLACE_MARK_72 中的字符串组合在1起 |
CODE_TAG_REPLACE_MARK_73 | 将 CODE_TAG_REPLACE_MARK_22 分成3份, CODE_TAG_REPLACE_MARK_44 左边, CODE_TAG_REPLACE_MARK_44 和 CODE_TAG_REPLACE_MARK_44 右边 |
CODE_TAG_REPLACE_MARK_78 | 和 CODE_TAG_REPLACE_MARK_79 相同,但从右边开始 |
CODE_TAG_REPLACE_MARK_80 | 将 CODE_TAG_REPLACE_MARK_22 中的 CODE_TAG_REPLACE_MARK_82 换成 CODE_TAG_REPLACE_MARK_83 ,若指定num,则替换不超过num次 |
Except for
'i love u'.title()
4
, you can also pass the
'i love u'.title()
5
To replace, the difference is that
'i love u'.title()
5
The input is
'i love u'.title()
7
Created replacement table.
>>> trans = ''.maketrans('I'," I ")
>>> "I Love U".translate(trans)
' I Love U'
format Formatting
'i love u'.title()
8
You can set the identifier
{}
Replace with the value in the tuple, if
{}
If no sequence number is specified in, the order is replaced.
'{} Love {}'.format('I','U')
'I Love U'
If
{}
If the sequence number is specified in, replace it in the order of sequence number
>>> '{0} Love {1}, {1} Love {0}'.format('I','U')
'I Love U, U Love I' # Recently brainwashed by Honey Snow Ice City. . .
Of course, like C language 1, this formatting is used for digital conversion more than 1, which is passed in python
'I Love U'.swapcase()
2
To declare the format after the number is converted to a string
Among them,
'I Love U'.swapcase()
3
They represent center, left-aligned, and right-aligned, respectively
'I Love U'.swapcase()
4
Indicates that + is displayed before positive numbers and-is displayed before negative numbers
b
,
'I Love U'.swapcase()
6
,
'I Love U'.swapcase()
7
,
'I Love U'.swapcase()
8
They are binary, decimal, octal and 106-ary respectively
Specific examples are as follows
>>> from math import pi
输入 | 输出 | 描述 |
---|---|---|
CODE_TAG_REPLACE_MARK_99 | ‘3.14' | 保留两位小数,4舍5入 |
CODE_TAG_REPLACE_MARK_100 | ‘314.16%' | 保留两位小数的百分数 |
CODE_TAG_REPLACE_MARK_101 | ‘+1.00' | 保留两位小数,带符号 |
CODE_TAG_REPLACE_MARK_102 | ‘00001' | 左侧补0,宽度为5 |
CODE_TAG_REPLACE_MARK_103 | ‘01' | 左侧补空格,宽度为5 |
CODE_TAG_REPLACE_MARK_104 | ‘1xxxx' | 右侧补x,宽度为5 |
CODE_TAG_REPLACE_MARK_105 | ‘–1--' | 两侧补-,宽度为5 |
CODE_TAG_REPLACE_MARK_106 | ‘100,000.0' | 逗号分隔 |
CODE_TAG_REPLACE_MARK_107 | ‘1.00e+05' | 科学计数法 |
CODE_TAG_REPLACE_MARK_108 | ‘1000' | 2进制 |
format_map
'i love u'.title()
8
Is a string tuple, replacing the string in the tuple with the string in the string according to the index
{}
, and
r
11
It is formatted directly with a dictionary.
For example, you can do it with tuples
>>> '{} Love {}'.format(" Jane "," A Qiang ")
' Jane Love A Qiang '
In a dictionary, you can write like this
>>> '{A} Love {B}'.format_map({"A":" Jane ","B":" A Qiang "})
' Jane Love A Qiang '
Thus making the code clearer.