美文网首页
第3章 使用字符串

第3章 使用字符串

作者: 有斧头的IceBear | 来源:发表于2022-03-26 23:28 被阅读0次

代码清单3-1 字符串格式设置示例

# Print a formatted price list with a given width

width = int(input('Please enter width: '))

price_width = 10
item_width  = width - price_width

header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
fmt        = '{{:{}}}{{:>{}.2f}}'.format(item_width, price_width)

print('=' * width)

print(header_fmt.format('Item', 'Price'))

print('-' * width)

print(fmt.format('Apples', 0.4))
print(fmt.format('Pears', 0.5))
print(fmt.format('Cantaloupes', 1.92))
print(fmt.format('Dried Apricots (16 oz.)', 8))
print(fmt.format('Prunes (4 lbs.)', 12))

print('=' * width)

运行结果如下:

Please enter width: 35
===================================
Item                          Price
-----------------------------------
Apples                         0.40
Pears                          0.50
Cantaloupes                    1.92
Dried Apricots (16 oz.)        8.00
Prunes (4 lbs.)               12.00
===================================

相关文章

  • Python基础教程Ch3-使用字符串

    第3章 使用字符串 ![](http://picture-repository-of-heamon7.qiniud...

  • part2. 函数| 汇总聚集

    第7章 创建计算字段 拼接字段&使用别名SQL: RTRIM():去掉字符串右边的空格LTRIM():去掉字符串左...

  • AI 学习之路——轻松初探 Python 篇(三)

    这是「AI 学习之路」的第 3 篇,「Python 学习」的第 3 篇 Python 字符串使用和 C 语言比较类...

  • Python编程:从入门到实践学习笔记

    第2章 字符串 使用方法修改字符串的大小写 方法名功能title()首字母大写upper()全部大写lower()...

  • 字符串与JSON

    1.使用数组拼接出如下字符串 2.写出两种以上声明多行字符串的方法 例如: 这段字符串很长,如何多行优雅的显示 第...

  • 「Redis设计与实现」字符串篇

    字符串存储规则 redis没有默认使用c字符串,仅在字符串字面量和使用c字符串。如果字符串为变量时,则使用SDS字...

  • JSON字符串-代码实例

    1. 使用数组拼接出如下字符串 直接拼接法 储存为数组转为字符串法 2. 写出两种以上声明多行字符串的方法 第1种...

  • 006-字符串

    字符串 字符串是Python中最常用的数据类型。一般使用引号来创建字符串 使用单引号创建字符串'hello' 使用...

  • Learning C Primer Plus | Chapter

    第 4 章 字符串和格式化输入/输出 前导程序 该程序有如下新特性: 使用一个数组(array)来存放字符串...

  • AppleScript-字符串

    使用规则字符串需使用双引号,不能使用单引号字符串中有引号时,需使用转义字符"",如"\他说:"你好!"" 字符串拼...

网友评论

      本文标题:第3章 使用字符串

      本文链接:https://www.haomeiwen.com/subject/vydejrtx.html