【问题标题】:How to form a string from 2 values如何从2个值形成一个字符串
【发布时间】:2012-06-28 20:22:29
【问题描述】:

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

F:0.0

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

root = str(newickString[-1])

float 0.0 可以这样:

rootD = 0.0

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

【问题讨论】:

  • “一种明显的方式”就这么多了。

标签: python string variables floating-point


【解决方案1】:
>>> string = 'WWF'
>>> num = 0.0
>>> print ("{0}:{1}".format(string[-1],num))
F:0.0

在旧版本的 Python (

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

而不是

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

【讨论】:

    【解决方案2】:

    您可以只使用+ 符号并将它们连接在一起:

    >>> old_string = 'oldF'
    >>> float_val = 0.0
    >>> rootString = old_string[-1] + ':' + str(float_val)
    >>> print rootString
    F:0.0
    

    【讨论】:

      【解决方案3】:

      Python 中的字符串插值非常简单。对于大多数版本的 Python,您可以编写:

      template = "%s:%f"
      root = "F"
      rootD = 0.0
      result = template % (root, rootD)
      # and result is "F:0.0"
      

      看看http://docs.python.org/library/stdtypes.html#string-formatting

      (请注意,如果您使用的是足够新的 Python 版本,您可能更愿意在字符串上使用较新的 .format 方法——请参阅 http://docs.python.org/library/string.html#new-string-formatting

      【讨论】:

        【解决方案4】:

        试试这个:

        '%s:%0.1f' % (root, rootD)
        

        Read more about string formatting here.

        【讨论】:

          【解决方案5】:

          如果您想要一个将其存储为字符串的变量(或建议的其他几种方法):

          rootString = "%s:%s" % (root,rootD)
          

          如果您打算稍后使用/更改该值,最好设置一个类似此示例代码的类(另外您仍然可以轻松打印字符串):

          class rootString:
              def __init__(self,root,rootD):
              self.root = root
              self.rootD = rootD
          
              def getRoot(self):
              return self.root
          
              def getRootD(self):
              return self.rootD
          
              def setRoot(self, root):
                  self.root = root
          
              def setRootD(self, rootD):
                  self.rootD = rootD
          
              def __str__(self):
              return "%s:%s" % (self.root,self.rootD)
          
          if __name__=='__main__': 
              myRootStr = rootString("F",0.0)
              print myRootStr #gives string output you want                                                                                                                                                                                                                             
              print myRootStr.getRootD() #but you can still get the float easily                                                                                                                                                                                                        
              myRootStr.setRootD(3.14) #or change it if you need to                                                                                                                                                                                                                     
              print myRootStr
          

          【讨论】:

            【解决方案6】:

            这是一种组合两个值并打印出来的方法。

            string_value = 'BOF'
            float_value = 0.0
            print "%s:%s" % (string_value[-1], float_value)
            >>F:0.0
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-04-02
              • 1970-01-01
              • 1970-01-01
              • 2020-12-11
              • 1970-01-01
              • 2023-03-26
              • 2022-01-19
              • 2019-01-07
              相关资源
              最近更新 更多