我想以两个单词的格式分配字符串,后跟相应的变量,如:
"newstring = 'length', length, 'slope', slope"
我将在text = Text(point1, newstring)
这样的文本函数中使用它
有没有办法实现这个目标?
答案 0 :(得分:1)
使用format:
>>> length=22
>>> slope=45
>>> newstring='length {}, slope {}'.format(length, slope)
>>> newstring
'length 22, slope 45'
格式函数或格式方法有一组丰富的format specifications,允许以所需的方式显示字符串:
>>> 'Feeling hexey 0x{:X} and octally 0{:o} for decimal {}'.format(12, 12, 12)
'Feeling hexey 0xC and octally 014 for decimal 12'
>>> "Lot's o decimals: {:0.30f}".format(.5)
"Lot's o decimals: 0.500000000000000000000000000000"
您还可以将字符串类型连接在一起:
>>> 'length ' + str(length) + ' slope ' + str(slope)
'length 22 slope 45'
或者使用年龄较大的肉丸'操作者:
>>> 'length %d, slope %d' % (length, slope)
'length 22, slope 45'
但这些并不像format
...