FAQ: Learn Python – Strings & Console Output – String Formatting with %, Part 1

This community-built FAQ covers the “String Formatting with %, Part 1” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy exercise String Formatting with %, Part 1:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try searching for it by clicking the spyglass icon (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

2 posts were split to a new topic: Printing in Python 3

2 posts were split to a new topic: Can I Print the Same Variable Twice?

For the "Strings & Output - String Formatting with %, Part 1, why is there no need to put a \ nest to the apostrophes in the string?

1 Like

You mean here?

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

That single quote (apostrophe) does not need escapement since the string is delimited by double quotes. We would only escape a quote if the string is delimited with the same thing.

'Let\'s all go to the market!'

is one example.

“I tells you”, he said, “that dern fella was yellin’, “Get off my lawn!”, and we was all startled.”

Why aren’t the "%"s colored differently than the string in the console? It seems inconsistent with the way that the rest of the console makes other things stand out.

it also works when i tried using double quotes…
print "Let’s not go to %s. 'Tis a silly %s. " %(string_1,string_2)

I’m learning Python right now and I just ran into String Formatting with % Part 1
Example:

name = “Mike”
print “Hello %s” % (name)

But I thought I remembered using another function “.format” and “{}”'s to do this
Is it because CodeAcademy is using teaching Python 2 and I learned about “.format” while studying Python 3 or are they two completely different things used in they’re own respects?

Older iterations of the course were exclusively Python 2.7 and at that time the newer .format method was not introduced, only the modulo method. It is still a supported method in Python 3 but less favored since it is not as robust and flexible as the later, .format and now with 3.6 the newest, f-string method.

At this point it is all new so take it in and practice with it so you can impress it on your brain and recognize it in older code.

https://pyformat.info/

The above page has as yet only comparison of the modulo method and the format method. One suspects it will eventually include comparisons of all three.

I think there is a bit of information overload in this exercise. The Learn section starts at an okay pace introducing %s, but then it discusses using % for integers and the concept of padding integers, all in the same breath. It feels like these could be left for later - neither of these slightly confusing new ideas figure in the exercise.

(I say slightly confusing because if I see that second paragraph about padding integers as a person new to coding, my first response is to ask “what does ‘padding’ mean?” I can pick it up by looking at the example, but it’s a bit overwhelming all at once)

We’re often advised to reach for the internet to find better explained and more detailed examples. It’s not uncommon for learners to see this as cheating but it is not cheating if you seek understanding, and not just an answer.

see pyformat link higher up in this topic

The above link fairly covers two forms of string formatting, including modulo format. It gives good examples and explanations. It would be perfectly alright to refer to this page to gain better understanding going forward in the lessons.

Lesson authors are limited in how much information they can include in the narrative, or how many lessons they may spread a concept over. Compromises have to be made. Just know that search is as much a part of the learning process as the narratives and exercises provided in each course.

Also recommended in most courses is the documentation for the given language. python.org is at first overwhelming but soon enough we learn how to track down important information relating to syntax and usage. Avail yourself of these resources and become an expert at tracking down clues and aids to learning.

I cannot run this code on pycharm…why is that…i get this instead…

what python version are you using? Have you verified its the same one you learned? If not, have you researched the differences?

Recall that in Python 2, print is a construct, whereas in Python 3 it is a function. Functions need to be invoked with () (parens on argument).

isnt %s a placeholder for only string type variables? How can it be used for integers as shown in this example in this exercise:

day = 6
print “03 - %s - 2019” % (day)
# 03 - 6 - 2019

There are a number of format specifiers in the old printf % style of formatting (see the docs for a list). Using %s just effectively casts the object as a string. As far I’m aware and a light glance at the docs state this is the almost or exactly the same as performing str(object) before printing.
https://docs.python.org/2/library/stdtypes.html#string-formatting-operations

1 Like

string_1 = “Beer”

string_2 = “Oslo”

print "Let’s have a %s in downtown %s.I love %s in %s. Because I’ve never tried it before. Looking forward to lick some %s " % (string_1, string_2);
Why above syntax is wrong?

Does your interpreter provide details of your error? I can see an error being thrown as several strings are expected but only two are provided. There is no obvious way to interpret what you have passed it so an error is thrown instead.

I quite like the following link for quick details about formatting- https://pyformat.info/
Have a look at the .format method for example which is generally preffered over the old % style formatting even for Python2.

What is the difference between this and the concatenating way ?