系统:windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
翻翻以前写的文章,发现字符串写过一次了,尴尬
<code class="javascript">X = "Yes;Y = "No"Z = "He is very handsome, {0}, {1}".format(X, Y)print(Z)</code><code class="javascript">X = "Yes";Y= "No"Z = "He is very handsome, {0}, {1}".format(X, Y)print(Z)</code>

{}起到占位作用,{0}的数字与format后面的值对应,从0开始format前的字符串无需和format后的值一一对应,{}中的数值不要超过format后的变量数目-1即可Part 2:{var}代码语言:javascript代码运行次数:0运行复制<code class="javascript">Z = "He is very handsome, {var1}, {var2}".format(var2="Yes", var1="No")print(Z)</code><code class="javascript">Z = "He is very handsome, {var1}, {var2}".format(var2="Yes", var1="No")print(Z)</code>

<code class="javascript">Z = "pi is {:.2f}".format(3.1415926)print(Z)</code><code class="javascript">Z = "pi is {:.2f}".format(3.1415926)print(Z)</code>保留2位小数

不保留小数
立即学习“Python免费学习笔记(深入)”;

百分数

带+

以上就是Python-入门-10-字符串常见函数(下篇)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号