-1

Hi I am trying to print this function but the output comes out with brackets and quotation marks... like this

('1', ',December', ',1984')

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))
Stardom
  • 23
  • 1
  • 6

4 Answers4

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

With this code, you're creating a tuple and not a string.

To create a string instead, you have multiple options:

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

OR

date = '%s %s, %s' % (day_num, month_name, year_num) # Fairly outdated

OR use + for concatenation, as per the other answers. Using + for string concatenation is not ideal, as you must make sure you convert each operand to a string type.

entropy
  • 840
  • 6
  • 16
0

The problem is you have some commas , where you need plus signs +:

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

This is creating a tuple instead of a string. Change it to:

    date = str(day_num) + "," + month_name + "," + str(year_num)
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0

Try changing the , to a + when creating the variable date. This creates a string rather than a list.

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))
bb8
  • 190
  • 1
  • 10
0

If you are using Python version 3.6 or newer, you might use so-called f-strings for that task, following way

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

Keep in mind that it doesn't work with older versions of Python, so please check your version before using it. If you want to know more about f-strings read this article.

Daweo
  • 31,313
  • 3
  • 12
  • 25