【问题标题】:How to return a string without quotes in a function?如何在函数中返回不带引号的字符串?
【发布时间】:2020-08-04 18:48:18
【问题描述】:

这是我的代码的一部分:

def LeapYear(year=0,month=0,day=0):
 .
 .
 .
  # as it's a long code I've jumped the unnecessary things.
 .
 .
 .    
return ("Today is the {}th day of the year!".format(T))

它返回这样的字符串(带引号):

'Today is the 70th day of the year!'

但我希望它像这样返回(不带引号):

Today is the 70th day of the year!

我不想使用函数来执行此操作,并且由于我有很多这样的行,我不希望解决方案很长,有没有办法?

【问题讨论】:

  • 引号不是字符串的一部分。如果你print它,引号将不会出现。
  • 没有引号是什么意思?它是一个字符串,并且字符串在 python 中总是用“引号”表示。你能描述一下你想在这里做什么吗?
  • 只有打印出来才能看到字符串;单引号 not 是返回值的一部分;当需要在交互式解释器中显示值的表示时,它们由 str.__repr__ 添加。
  • @Moty.R It's python syntax。另外,为什么要返回一个冗长的字符串,打印年份并将年份返回为Int

标签: python function return


【解决方案1】:

是的,它返回带引号的字符串,因为这就是字符串的表示方式。但是,如果您打印它,例如print(LeapYear) 将正常打印它,Today is the 70th day of the year!

另一个例子是:你将如何定义一个字符串变量?是这样的:

string = 'Example String'

不是这个:

string = Example String

如果你这样做了

print(string)

它将打印Example String,不带引号。

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2018-09-10
    • 2019-02-12
    • 2016-02-14
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多