python - 使用 python3 进行字符串格式打印:有时会从解压的数组中打印
问题描述
在几分钟前str.format
的问题中,当字符串存储在数组中时,我询问了如何使用 python 的打印进行打印。
然后答案显然是解包列表,如下所示:
# note that I had to play with the whitespace because the {} text is 2 characters, while its replacement is always one
hex_string = r'''
_____
/ \
/ \
,----( {} )----.
/ \ / \
/ {} \_____/ {} \
\ / \ /
\ / \ /
)----( {} )----(
/ \ / \
/ \_____/ \
\ {} / \ {} /
\ / \ /
`----( {} )----'
\ /
\_____/
'''
letters = list('1234567')
print(hex_string.format(*letters))
但是,如果我总是希望中心六边形打印在数组的第一项内:letters[0]
,我该如何混合解包数组和保留第一个数组元素的第 4 个打印字符串?
我对其他打印类型持开放态度,例如 f 字符串。
例如:
print(hex_string.format(letters[3], letters[1], letters[2], letters[0], letters[4], letters[5], letters[6]))
所以我的输出实际上是这样的:
_____
/ \
/ \
,----( 4 )----.
/ \ / \
/ 2 \_____/ 3 \
\ / \ /
\ / \ /
)----( 1 )----(
/ \ / \
/ \_____/ \
\ 5 / \ 6 /
\ / \ /
`----( 7 )----'
\ /
\_____/
解决方案
你可以尝试这样的事情.format()
:
a = '123'
print('{2}, {0}, {1}'.format(*a))
这将打印3, 1, 2
使用这种方法,您的首字母hex_string
将“自我记录”,您的数组中的字母将精确到哪里。
推荐阅读
- javascript - Jquery-如何在两个第 n 个子标签中获得相同的数字?
- oop - Visual Studio Code:在手动添加到原型的方法上使用“转到定义”
- ruby-on-rails - rails 将参数或变量传递给多个控制器操作
- javascript - 在 React 中旋转图像
- twilio - 如何使用 Twilio SIP 端点设置语音邮件
- elasticsearch - 在 Elastic Search 中带有附加条件的 Completion Suggester
- python-3.x - 如何在熊猫中一次选择多行?
- php - 即使记录不存在于另一个表中,也可以在 JOIN 查询中显示 MySQL 行
- xcode - GitLab CI Build 机器上的 Xcode Provisioning Profile 问题
- dotnetbrowser - 在 Dotnetbrowser 中没有清除 browsercore32 进程