Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输出带有括号和引号_Python - Fatal编程技术网

Python 输出带有括号和引号

Python 输出带有括号和引号,python,Python,您好,我正在尝试打印此函数,但输出带有括号和引号。。。像这样 (‘1’、‘12’、‘1984’) 问题是在需要加号的地方有一些逗号,: date = str(day_num) , "," + month_name , "," + str(year_num) 这是创建元组而不是字符串。将其更改为: date = str(day_num) + "," + month_name + "," + str(year_num) 创建变量日期时,尝试将,更改为+。这将创建一个字符串而不是列

您好,我正在尝试打印此函数,但输出带有括号和引号。。。像这样

(‘1’、‘12’、‘1984’)


问题是在需要加号的地方有一些逗号

    date = str(day_num) , "," + month_name , "," + str(year_num)
这是创建元组而不是字符串。将其更改为:

    date = str(day_num) + "," + month_name + "," + str(year_num)

创建变量
日期时,尝试将
更改为
+
。这将创建一个字符串而不是列表

def date_string(day_num, month_name, year_num):
""" 
        Turn the date into a string of the form
        day month, year
"""
date = str(day_num) + ", " + month_name + ", " + str(year_num)
return date
print(date_string(1, "December", 1984))
使用这段代码,您将创建一个元组而不是字符串

要改为创建字符串,您有多个选项:

date = '{} {},{}'.format(day_num, month_name, year_num) # Recommended method


或者根据其他答案,使用
+
进行连接。使用
+
进行字符串连接并不理想,因为您必须确保将每个操作数转换为字符串类型。

如果您使用的是Python版本
3.6
或更高版本,则可以使用所谓的f字符串来执行该任务,如下所示

def date_string(day_num, month_name, year_num):
    date = f"{day_num},{month_name},{year_num}"
    return date
print(date_string(1, "December", 1984)) #output: 1,December,1984
请记住,它不适用于较旧版本的Python,因此请在使用之前检查您的版本。如果您想了解更多有关f字符串的信息,请阅读

date = '{} {},{}'.format(day_num, month_name, year_num) # Recommended method
date = '%s %s, %s' % (day_num, month_name, year_num) # Fairly outdated
def date_string(day_num, month_name, year_num):
    date = f"{day_num},{month_name},{year_num}"
    return date
print(date_string(1, "December", 1984)) #output: 1,December,1984