Python字符串格式化输出
2017-10-10 22:53
525 查看
在Python可以使用字符串的format函数替换掉字符串中的
示例代码:
输出
Enter amount: 10000
Enter Interest rate: 0.2
Enter period: 10
Year 1 Rs. 12000.00
Year 2 Rs. 14400.00
Year 3 Rs. 17280.00
Year 4 Rs. 20736.00
Year 5 Rs. 24883.20
Year 6 Rs. 29859.84
Year 7 Rs. 35831.81
Year 8 Rs. 42998.17
Year 9 Rs. 51597.80
Year 10 Rs. 61917.36
Total = 61917.364
输出
Fahrenheit Cesius
0 -17.78
25 -3.89
50 10.00
75 23.89
100 37.78
125 51.67
150 65.56
175 79.44
200 93.33
225 107.22
250 121.11
{}格式化描述符号从而达到C中的printf效果
示例代码:
#!/usr/bin/python3 amount = float(input("Enter amount: ")) inrate = float(input("Enter Interest rate: ")) period = float(input("Enter period: ")) value = 0 year = 1 while year <= period: value = amount + (inrate * amount) print("Year {} Rs. {:.2f}".format(year, value)) amount = value year += 1 print("Total = {:.3f}".format(value))
输出
Enter amount: 10000
Enter Interest rate: 0.2
Enter period: 10
Year 1 Rs. 12000.00
Year 2 Rs. 14400.00
Year 3 Rs. 17280.00
Year 4 Rs. 20736.00
Year 5 Rs. 24883.20
Year 6 Rs. 29859.84
Year 7 Rs. 35831.81
Year 8 Rs. 42998.17
Year 9 Rs. 51597.80
Year 10 Rs. 61917.36
Total = 61917.364
#!/usr/bin/python3 fahrenheit = 0 print("Fahrenheit Cesius") while fahrenheit <= 250: celsius = (fahrenheit - 32) / 1.8 print("{:5d} {:10.2f}".format(fahrenheit, celsius)) fahrenheit = fahrenheit + 25
输出
Fahrenheit Cesius
0 -17.78
25 -3.89
50 10.00
75 23.89
100 37.78
125 51.67
150 65.56
175 79.44
200 93.33
225 107.22
250 121.11
相关文章推荐
- python字符串格式化输出的方式
- Python - 格式化(format())输出字符串 详解 及 代码
- python字符串格式化输出
- Python字符串格式化输出方法分析
- 【Python④】python恼人的字符串,格式化输出
- python日期时间转为字符串或者格式化输出的实例
- Python入门(二)——IDE选择PyCharm,输入和输出,基础规范,数据类型和变量,常量,字符串和编码,格式化
- python中格式化字符串输出的方法总结
- python 堆、队列操作、字符串格式化输出、字符串常用函数 示例代码
- Python实现字符串格式化输出的方法详解
- Python 字符串格式化输出(format/printf)
- Python 字符串格式化输出(format/printf)
- 浅谈Python 字符串格式化输出(format/printf)
- Python 字符串 格式化 输出
- Python第一课,字符串的格式化输出
- Python字符串格式化输出方法分析
- 字符串输出格式化(Java、Python、JavaScript、Kotlin)
- Python - 格式化(format())输出字符串 详解 及 代码
- [Python 实战] - No.5 字符串、切片与格式化输出
- python字符串格式化输出及相关操作代码举例