首页 > python > 如何从2个值形成一个字符串

如何从2个值形成一个字符串 (How to form a string from 2 values)

问题

好吧,我需要创建一个定义为rootString的字符串,如下所示:

F:0.0

字母F源自先前的字符串,通过以下方式获得:

root = str(newickString[-1])

并且浮点数0.0可以是这样的:

rootD = 0.0

我的问题是,如何将变量名和浮点值与冒号结合起来?

解决方法

>>> string = 'WWF'
>>> num = 0.0
>>> print ("{0}:{1}".format(string[-1],num))
F:0.0

在旧版本的Python(<2.6)上,您需要执行以下操作:

"%s:%s" % (string[-1], num)

代替

"{0}:{1}".format(string[-1],num)

问题

Alright, i need to make a string defined as rootString that looks like this:

F:0.0

The letter F is derived from a previous string, obtained by:

root = str(newickString[-1])

And the float 0.0 can be as such:

rootD = 0.0

My question is, how do I combine both variable name and float value with a colon?

解决方法

>>> string = 'WWF'
>>> num = 0.0
>>> print ("{0}:{1}".format(string[-1],num))
F:0.0

On older versions of Python (<2.6) you will need to do:

"%s:%s" % (string[-1], num)

instead of

"{0}:{1}".format(string[-1],num)
相似信息