【问题标题】:Understanding print in return statements了解返回语句中的打印
【发布时间】:2019-05-19 06:23:13
【问题描述】:

return 语句返回额外的撇号和括号,我不知道为什么。

此代码查找字符串中是否存在子字符串。

    def find(the_string, search_this):
     if search_this in the_string:
         a = the_string.find(search_this)
         # returns the unexpected 
         return (search_this, "found at", str(a))
     else:
         # the correct output I am looking for
         return (search_this + " was not found at " + the_string)

     print(find("qweabc","abc"))
     print(find("abcd", "xyz"))

第一个 return 语句向我返回了一个不可取的 print 语句。

示例:('abc', 'found at', '3')

第二个 return 语句返回我正在寻找的 print 语句:

例如:xyz was not found at abcd

打印出来时,为什么第一个return语句有多余的括号和撇号?

【问题讨论】:

  • 如果您想知道为什么两个回报给出不同的东西,您是否尝试过比较它们?两者有明显区别。
  • @jonrsharpe 你的意思是比较两个返回语句的输出吗?是的,已经通过打印出来进行了比较,但是输出不应该相同吗?
  • 不,我的意思是实际上查看代码。您有两条不同的线路,您希望它们做同样的事情,但做不同的事情。那么你有没有检查过 are 行是否相同?它们之间有什么区别?如果你让它们更相似,问题是否仍然存在?
  • 您好,您的问题解决了吗?如果是这样,请随时将答案标记为已接受。

标签: python return


【解决方案1】:

当您使用return (search_this, "found at", str(a)) 时,您正在创建一个tuple

你可以这样做(Python 2.6 或以上):

return "{} found at {}".format(search_this, str(a))

或者你可以这样做(Python 3.6 或以上):

return f"{search_this} found at {str(a)}"

测试你的例子:

def find(the_string, search_this):
    if search_this in the_string:
        a = the_string.find(search_this)
        return f"{search_this} found at {str(a)}"
    else:
        return (search_this + " was not found at " + the_string)

print(find("qweabc","abc"))
print(find("abcd", "xyz"))


output:
abc found at 3
xyz was not found at abcd

【讨论】:

    【解决方案2】:

    这个表达式创建了一个由三个字符串组成的tuple。在 Python 中,tuple 类似于列表:

    In [138]: ('one', 'two', 'three')                                            
    Out[138]: ('one', 'two', 'three')
    

    这个表达式将三个字符串连接成一个字符串:

    In [139]: ('one'+ 'two'+ 'three')                                            
    Out[139]: 'onetwothree'
    

    () 在这种情况下只是一个分组工具,不要做任何更改:

    In [140]: 'one'+ 'two'+ 'three'                                              
    Out[140]: 'onetwothree'
    

    要使用一个项目(例如字符串)创建tuple,您必须包含一个逗号:

    In [141]: ('one'+ 'two'+ 'three',)                                           
    Out[141]: ('onetwothree',)
    

    实际上,创建元组的是逗号(比() 更重要)

    In [142]: 'one', 'two', 'three'                                              
    Out[142]: ('one', 'two', 'three')
    

    为了比较一个列表:

    In [143]: ['one', 'two', 'three']                                            
    Out[143]: ['one', 'two', 'three']
    

    字符串、元组和列表的这种表示法一开始可能会让人困惑,但值得好好学习。

    还有另一种变体 - 将三个字符串传递给 print 函数:

    In [144]: print('one', 'two', 'three')                                       
    one two three
    

    【讨论】:

      【解决方案3】:

      你想替换 return (search_this, "found at", str(a))return (search_this + "found at" + str(a))

      或者最好是:return "{} found at {}".format(search_this, a)

      【讨论】:

        猜你喜欢
        • 2022-08-16
        • 2014-12-27
        • 1970-01-01
        • 2023-01-13
        • 2012-08-06
        • 2013-11-05
        • 1970-01-01
        • 1970-01-01
        • 2020-08-17
        相关资源
        最近更新 更多