多行字符串返回语句python [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多行字符串返回语句python [关闭]相关的知识,希望对你有一定的参考价值。

我有一个简短但可能不简单的问题。

如何格式化此代码以避免错误?

def __str__(self):
    return  "
Team name is " + 
            self.teamName + 
            "
Their regular season record was " + self.record +
            "
Their win streak is " + self.winStreak +
            "
Their scoring percentage is " +
             self.scoringPercentage +
            "
Their average rebounds are " + 
            self.rebounds +
            "
Their passing is " + self.passing +
            "
Their turnovers are " + self.turnovers

IDLE给出了语法错误。

答案

我没有看到给出的fstring示例(Python 3.6+):

>>> from textwrap import dedent
... 
... 
... class Hmm:
...     def __init__(self):
...         self.team_name = 'TEAM'
...         self.record = 'RECORD'
...         self.win_streak = 'WINSTREAK'
...         self.scoring_percentage = 'SCORING_PERCENTAGE'
...         self.passing = 'PASSING'
...         self.rebounds = 'REBOUNDS'
...         self.turnovers = 'TURNOVERS'
... 
...     def __str__(self):
...         return dedent(f"""
...             Team name is {self.team_name}
...             Their regular season record was {self.record}
...             Their win streak is {self.win_streak}
...             Their scoring percentage is {self.scoring_percentage}
...             Their average rebounds are {self.rebounds}
...             Their passing is {self.passing}
...             Their turnovers are {self.turnovers}""")
... 
>>> print(Hmm())

Team name is TEAM
Their regular season record was RECORD
Their win streak is WINSTREAK
Their scoring percentage is SCORING_PERCENTAGE
Their average rebounds are REBOUNDS
Their passing is PASSING
Their turnovers are TURNOVERS
另一答案

我们应该尽可能地避免使用+运算符和字符串,因为每个+可能会创建一个中间字符串。对多行字符串使用"""(双引号)或'''(单引号)或使用分隔它们。

注意:应谨慎使用"""''',否则可能会在两条线之间打印太多空格。

使用格式或类似概念来格式化字符串。

def __str__(self):
    return  "Team name is {0}
"
            "Their regular season record was {1}
"
            "Their win streak is {2}
"
            "heir scoring percentage is {3}
"
            "Their average rebounds are {4}
"
            "Their passing is {5}
"
            "Their turnovers are {6}".format( self.teamName,
            self.record, self.winStreak, 
            self.scoringPercentage,self.rebounds,
            self.passing, self.turnovers )

使用"""

def __str__(self):
        return  """Team name is {0}
Their regular season record was {1}
Their win streak is {2}
Their scoring percentage is {3}
Their average rebounds are {4}
Their passing is {5}
Their turnovers are {6}""".format( self.teamName,
self.record, self.winStreak, self.scoringPercentage, self.rebounds,self.passing, self.turnovers ) 

使用带关键字参数的格式。

def __str__(self):
        return  """Team name is {name}
Their regular season record was {record}
Their win streak is {streak}
Their scoring percentage is {score}
Their average rebounds are {rebounds}
Their passing is {passing}
Their turnovers are {turnovers}""".format( name=self.teamName,
record=self.record, streak=self.winStreak, score=self.scoringPercentage,
rebounds=self.rebounds,passing=self.passing, turnovers=self.turnovers ) 

使用字符串格式样式,如C / C ++。

    def __str__(self):
       return  """Team name is %s
Their regular season record was %.2f
Their win streak is %s
Their scoring percentage is %s
Their average rebounds are %s
Their passing is %s
Their turnovers are %s"""%( self.teamName,
   self.record, self.winStreak, self.scoringPercentage, 
   self.rebounds,self.passing, self.turnovers )

https://ideone.com/xJ31fN

另一答案

在Python 2和3中有效的选项是使用join()函数

def __str__(self):
    out = ['
Team name is ' + self.teamName]
    out.append('Their regular season record was ' + self.record)
    out.append('Their win streak is ' + self.winStreak)
    out.append('Their scoring percentage is ' + self.scoringPercentage)
    out.append('Their average rebounds are ' + self.rebounds)
    out.append('Their passing is ' + self.passing)
    out.append('Their turnovers are ' + self.turnovers)
    return '
'.join(out)

以上是关于多行字符串返回语句python [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Python for循环语句

Python exec 函数

Python for循环使用 else 语句

语法基础

SQL语句execute后的参数

20个简洁的 JS 代码片段