String Formatting with %, Part 1


#1



https://www.codecademy.com/en/courses/python-beginner-sRXwR/3/3?curriculum_id=4f89dab3d788890003000096


Hello, I was doing this excercise, everything worked fine. But I noticed, there is no backslash in the word "Let's not...".


As described in the excercise few lessons back, there should be backslash like "Let\'s not..." so the string would not break.

Why is that so? Is it because the string begins and ends with "double" quotation mark instead of "single" one?

Thank you in advance.


string_1 = "Camelot"
string_2 = "place"

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


#2

if you would have used apostrophes to mark the beginning and end of the string:

'a string with apostrophe'

and you would have an apostrophe in the sentence, you need to escape it, otherwise your string ends prematurely. If you use quotation marks to mark the beginning and of the string, you don't need to escape apostrophes (you would need to escape quotation marks, obviously)


#3

Thank you stetim94!

One more question. Is there ANY significant difference between using apostrophes and quotation marks for beginning and ending of a string?

"This is a string" vs. 'This is also a string'


#4

apart from the escaping bit we just talked about? No, you can use both.

(if you have a string which a lot of quotes for which you use quotation marks, enclose the string in apostrophes)


#5

And vice versa, I understand. It makes a little more sense. I'm new to programming and haven't developed this "thinking style".

You've been very helpful, thank you.


#6

Learn to code -> easy
Learn to program -> Difficult

Part of programming is the thinking style/mind set. Good luck!


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.