String formatting with %, Part 1


#1

https://www.codecademy.com/courses/learn-python/lessons/strings--console-output/exercises/string-formatting-with--part-?action=resume_content_item

Hi all, I have a silly question so I apologise. Just want to try and take advantage of forums and really be a part of the community.
With this particular exercise, I thought that apostrophes in the middle of your string confuses the system into thinking it’s the end of the string… which is why we’re taught previously to put in a backslash () before the apostrophe to allow the string to continue.
Why did it accept the apostrophe before “Tis” without ending the string?

Again, I apologise if it’s silly or if someone has brought it up previously (so…many…topics…).

Thanks!

Cleone


#2

the beauty of python, given we can use both apostrophes (') and quotation marks (") to enclose the string, if we enclose the string with quotation marks (like you did), we can safely insert apostrophes within the string

however, if you enclose the string with apostrophes, then you can’t have apostrophes within the string, given it will end the string prematurely. Unless you use the \ to escape the apostrophe

What you currently have is recommend, and vice versa of course. If your string where to have quotation marks (for quotes) you should enclose the string in apostrophes.


#3

Ahh that makes sense, I should have realised I was using quotation marks rather than apostrophes for the string. A previous lesson did indicate that a string has to have either quotations or apostrophes but not both in order for it to work.

That extra point about having quotes within the string is really useful as well, thank you!
:slightly_smiling_face:

Btw, could you tell me how you got the apostrophes/quotations/backslash symbols to show in your comment? I tried in my original post and it didn’t show.


#4

You need to format your code in order for ' and " to show:

How do I format code in my posts?


#5

Thank you very much :slight_smile:


#6

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