How can I combine two print statements that both use string formatting?
Answer
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!
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.
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:
datetime.now() 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.
When writing a string and format arguments in Python 3, don’t close the outer parens until after the arguments list.
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.
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.
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