python 2.3*
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326382693&siteId=291194637
Recommended
Ranking
The python string formatting symbol %f specifies the precision after the decimal point.
Python string formatting symbols:
What does %g mean by shorthand for %f and %e? Is it %f or %e?
I tested this with the code:
It can be found that %g automatically selects the output format. In the case of six digits, it will be output in scientific notation. The article says that %g is the abbreviation of %f and %e, but I found that the above is not scientific notation. When the method is output, the output is an integer, so the following test is performed:
It is found that %g is still different from %f when it is not output with %e
I did the following test again:
发现在 a=100000.1 的时候输出的数并没有小数点后面的 1,对此我对比了 C 语言 %g 的格式输出,猜想 python 中应该如同 C 语言一样,%g 用于打印数据时,会去掉多余的零,至多保留六位有效数字。
使用格式化符号进行进制转换
上面使用格式化符号进行进制转换中,多加入了一个#号,目的是在转换结果头部显示当前进制类型,如不需要,可将#号去除,如下
字符串截取字符继续补充:
[::2] 表示的是从头到尾,步长为2。第一个冒号两侧的数字是指截取字符串的范围,第二个冒号后面是指截取的步长。
字符串的分割还有partition()这种方式。
从左向右遇到分隔符把字符串分割成两部分,返回头、分割符、尾三部分的三元组。如果没有找到分割符,就返回头、尾两个空元素的三元组。
结果如下:
("I'm a ", 'good', ' sutdent.') ("I'm a good sutdent.", '', '')针对 Counter 的升级使用,示例如下:
输出如下:
针对输出的结果,可以根据字典的定义进行其他必要的操作