How can I combine two print statements that both use string formatting?


How can I combine two print statements that both use string formatting?


The best way to demonstrate is with an example, check out the code below:

print “This is my first %s.” % (“string”)
print “But I will be combined %s“ % (“one day soon!”)

print “This is my first %s. But I will be combined %s” % (“string”, “one day soon!”)

We started off with two separate strings being formatted and simply combined the strings and variables being inserted. The lesson wants the same thing!


This works in Python 3? I’m using a 3.7.2 version and my script even fails

from datetime import datetime
tempo =
print (’%02d/%02d/%04d %02d:%02d:%02d’) % (, tempo.month, tempo.year, tempo.hour, tempo.minute, tempo.second)

You’re doing two very separate things there. String formatting, and printing a string.
If you do them separately you’ll have an easier time translating it to python3.
(solve one problem at a time, don’t turn two problems into one very complicated one where you can’t tell where one problem ends and the other begins)
There is no print statement in python 3. Instead, there is a function named print. So what you would need to change is how you print your string. The string formatting can remain the same.

Then how would it look?


:joy: thanks for nothing

Sometimes you get what you ask for

1 Like

Hi @jamesgomesl,

Regarding the above, let’s consider this task in two steps:

  • Format the output in Python 2, as specified in the Codecademy exercise.
  • Convert the Python 2 print command to a Python 3 print function call.

If you have already successfully submitted your code to Codecademy as a Python 2 script, could you post the line that displays the output? That will serve as the first of the two steps. Before replying, however, visit the following link, so that you can format the code for proper display in your post:

The section entitled Format Your Code will be helpful.

After you post your code, we can consider how to perform the conversion to a Python 3 print function call.

Two things to consider…

  1. returns 2 digits for everything except the year, which is 4 digits. Given that we do not need to fit the results to any constraints, the placeholder size is not necessary in modulo formatting.

    '%d/%d/%d %d:%d:%d'

    or even,

    '%s/%s/%s %s:%s:%s'

    will suffice. Everything in the argument list is cast to string in the output.

  2. When writing a string and format arguments in Python 3, don’t close the outer parens until after the arguments list.

    print ('...' % (...))
1 Like

It doesn’t return any number of digits, it has numbers, and those numbers are not necessarily greater than 10

I’m also very new to Python & programming ( 2 days!) So I felt like I could possibly answer your question in newbie words.

Python 3 - use print(“A”) while Python 2 uses print “A”

Like you, I was also trying to copy the codes onto Python 3.7.2 and got an error. Here’s what works afterwards.

Codes from Python 2
from datetime import datetime
now =
print ’ %02d:%02d:%04d’ % (now.month,, now.year)

Codes from Python 3
from datetime import datetime
now =
print (’%02d/%02d/%04d’ % (now.month,, now.year))

I also attempted to use format() in python3 but failed… hmmmmm a lot of new things to learn

1 Like

Yes, right you are.

>>> from datetime import datetime
>>> now =
>>> now
datetime.datetime(2019, 1, 15, 0, 0, 1, 60221)

That’s it, THANK YOU!!
I was thinking that this formatting that uses% no longer works. I ended up looking for another course in my language (I’m Brazilian) and learned the formatting with {} and .format.
Because I did not know how to apply the old syntax to Py3 I was going to abandon the codecademy. Now, I’m going back to the CC, but learning python 2 and 3 syntax simultaneously will be very confusing.
I’m also new to the programming world, but I already love Pyhton, his philosophies (Zen), and this community.

I was expecting something like this:

from datetime import datetime
tempo =
print(’%d/%d/%d’ % (, tempo.month, tempo.year))

Python 3 is backward compatible when it comes to string formatting. The modulo method is not as versatile as the newer format, or the latest, f-string syntax (3.7 and above).

This link predates f-string, but gives a good comparison of the two earlier methods.

I do that and it works also :

from datetime import datetime
now =
print ‘%02d/%02d/%04d’ % (now.month,, now.year) + ’ %02d:%02d:%02d’ % (now.hour, now.minute, now.second)

must be doing something wrong… cant get it to work

u just need to correctly format what u want to print. this is how it looks like for me

from datetime import datetime
now =

print ('%02d/%02d/%04d  %02d:%02d:%02d' % (now.month,, now.year,    now.hour, now.minute, now.second))

watch out for brakets and ’ things

what is s% and what is d% - and what is their etymology – because i cannot make sense of it when i don’t know - i get that there are some things that you just get to use and later its maybe not giving a second thought about it - excuse me if its a ■■■■ question but i’m absolute beginner, cheers

%__ is a format directive where % is the format operator and __ is the representation intended.

%s  =>  denotes string representation

%d  => denotes digit representation
%i  => denotes integer (same as digit)

%f  => denotes float representation

%r  => denotes reasonable string representation

There are other directives such as hex integer, octal integer, ASCII character, and so on.

Just found this page on the subject…

Give it a read and see if this becomes clear in your mind.

Note that this is the legacy formatting method of Python and has been joined by two newer methods, str.format() and f-string.