习题8 打印,打印

formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4))                                    # 1 2 3 4
print(formatter.format("one", "two", "three", "four"))                 # one two three four
print(formatter.format(True, False, False, True))                      # True False True False
print(formatter.format(formatter, formatter, formatter, formatter))    # {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
print(formatter.format(
    "Try your",                                                        # 注意后面的逗号
    "Own text here",
    "Maybe a poem",
    "Or a song about fear",
))                                                                     # Try you Own text here Maybe a poem Or a song about fear


"""
第8行第一次运行缺少逗号报错:

Traceback (most recent call last):                                     # 追溯(最近一次通话):
  File "E:/SoftWareSpace/stu_py/py_obj/笨方法学Python的代码/习题8 打印,打印.py", line 8, in <module>   # 第8行
    "Try your"                                                         # 语句
IndexError: tuple index out of range                                   # IndexError:元组索引超出范围
"""
"""
formatter.format(...)                                                  # format 格式化
1.取第1行定义的formatter字符串
2.调用他的format函数,这相当于告诉他执行一个交format的命令行命令
3.给format传递四个参数,这些参数和formatter变量中的{}匹配,相当于将参数传递给了format这个命令
4.在formatter上调用format的结果是一个新字符串,其中的{}被4个变量替换掉了
"""

formatter = "问题 {} {} {}"                                               # 数字1看来是不能当成一个空的抽屉,只有{}才能被替换

print(formatter.format(1, 2, 3, 4))                                    # 这里的运行结果为 问题 1 2 3  看来计算机不能识别位置,只能顺序装在盒子里
print(formatter)                                                       # 从这里可以看到,format的打印结果并没有被赋值,format还是format,老王还是老王
print(formatter.format("one", "two", "three", "four"))                 #
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))    # 现在的结果为 问题 问题 {} {} {} 问题 {} {} {} 问题 {} {} {} 哈哈 研究一下?
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear",
))

formatter = "问题 {} 坏蛋 {}"                                     # 这里运行结果为应该为 问题 问题 {} 坏蛋 {} 坏蛋 问题 {} 坏蛋 {}
print(formatter.format(formatter, formatter, formatter, formatter))    # 哈哈  看来是把formatter装到了formatter的盒子里,原来如此  看来你已经掌握这个小问题了。
formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4))                                    # 1 2 3 4
print(formatter.format("one", "two", "three", "four"))                 # one two three four
print(formatter.format(True, False, False, True))                      # True False True False
print(formatter.format(formatter, formatter, formatter, formatter))    # {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
print(formatter.format(
    "Try your",                                                        # 注意后面的逗号
    "Own text here",
    "Maybe a poem",
    "Or a song about fear",
))                                                                     # Try you Own text here Maybe a poem Or a song about fear


"""
第8行第一次运行缺少逗号报错:

Traceback (most recent call last):                                     # 追溯(最近一次通话):
  File "E:/SoftWareSpace/stu_py/py_obj/笨方法学Python的代码/习题8 打印,打印.py", line 8, in <module>   # 第8行
    "Try your"                                                         # 语句
IndexError: tuple index out of range                                   # IndexError:元组索引超出范围
"""
"""
formatter.format(...)                                                  # format 格式化
1.取第1行定义的formatter字符串
2.调用他的format函数,这相当于告诉他执行一个交format的命令行命令
3.给format传递四个参数,这些参数和formatter变量中的{}匹配,相当于将参数传递给了format这个命令
4.在formatter上调用format的结果是一个新字符串,其中的{}被4个变量替换掉了
"""

formatter = "问题 {} {} {}"                                               # 数字1看来是不能当成一个空的抽屉,只有{}才能被替换

print(formatter.format(1, 2, 3, 4))                                    # 这里的运行结果为 问题 1 2 3  看来计算机不能识别位置,只能顺序装在盒子里
print(formatter)                                                       # 从这里可以看到,format的打印结果并没有被赋值,format还是format,老王还是老王
print(formatter.format("one", "two", "three", "four"))                 #
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))    # 现在的结果为 问题 问题 {} {} {} 问题 {} {} {} 问题 {} {} {} 哈哈 研究一下?
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear",
))

formatter = "问题 {} 坏蛋 {}"                                     # 这里运行结果为应该为 问题 问题 {} 坏蛋 {} 坏蛋 问题 {} 坏蛋 {}
print(formatter.format(formatter, formatter, formatter, formatter))    # 哈哈  看来是把formatter装到了formatter的盒子里,原来如此  看来你已经掌握这个小问题了。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值