Hello everyone, I hope you are doing well today. Have you ever been writing out a string or a print statement in python and got stuck trying to put an apostrophe in there? If so, I have the answers for you. First, I am going to address our error… single tick hell… then I am going to give you the solutions to your problems and keep you out of single tick hell. Fear not citizen! Hope has arrived!
So, you are struggling with your single ticks, and you come across something like this:
The script doesn’t pick up that you are trying to put in an apostrophe unless you decide to use an excessive number of apostrophes like this.
But this doesn’t look very pleasant on the eyes. With that being said, let me offer a couple of alternatives.
First up we use the special character “\”. Using this in front of a character changes the meaning of said character. For example, \n tells the next part of the string or the next string to go to the next line. \t inserts a “tab” into the string. And for our solution, \’ allows the use of an apostrophe in a string without having to do all those extra apostrophes. It would look like this in the code.
Secondly, we have my personal favorite. This other way of using an apostrophe doesn’t force you to use a bunch of apostrophes/single ticks nor does it require the use of our special key “/”. An alternative to using single ticks to signify the opening and closing of a string, you can use double ticks/quotation marks. Your script would look like this.
At the end of the day any of these 3 can work but in terms of ease of use I would lean towards using the special character or double ticks to wrap the string. I hope that this was informative and that you learned something from my blog. Anyways, thank you for reading and have a great rest of your day!
Bailey McDonald
Data Engineer, Patriot Consulting
Email: bkmcdonald@patriotconsultingcorp.com | Blogs: Patriot Consulting Blogs
LinkedIn: Personal: BaileyMcDonald | Company: Patriot Consulting