In my 几分钟前提问 https://stackoverflow.com/q/56092475/2327328,我问的是如何使用python的打印str.format
当字符串存储在数组中时进行打印。
然后答案显然是解压列表,如下所示:
# 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]
,如何将数组解包与保留第一个数组元素中的第四个打印字符串混合在一起?
我对其他打印类型持开放态度,例如 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
将“自我记录”数组中的字母将准确到达的位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)