Bugs That Bite
Published in

Bugs That Bite

SyntaxError: EOL while scanning string literal

Error in a line with a python escape character

I’m writing a json template parser in python and go the following error:

SyntaxError: EOL while scanning string literal

The problem was that I was checking for a backslash in a string like this:

if line.startswith(“[\”):

The backslash is an escape character used in front of special characters within string so they will be processed correctly. For example, let’s say you want to print out a string with quotes in it like this:

The word is "test".

You need to escape the double quotes if they are inside a string wrapped in double quotes:

variable="The word is \"test\"."

Now returning to my string the command I was trying to process, python thinks that I’m trying to escape the second string instead of end the string.

if line.startswith("[\"):

In order to correctly interpret the the bracket and backslash I would need to escape the backslash itself with another backslash.

if line.startswith("[\\")

That causes the backslash to be processed as a backslash character instead of an escape character in front of a quote.

Teri Radichel

If you liked this story please clap and follow:

Medium: Teri Radichel or Email List: Teri Radichel
Twitter: @teriradichel or @2ndSightLab
Requests services via LinkedIn: Teri Radichel or IANS Research

© 2nd Sight Lab 2022

____________________________________________

Author:

Cybersecurity for Executives in the Age of Cloud on Amazon

Need Cloud Security Training? 2nd Sight Lab Cloud Security Training

Is your cloud secure? Hire 2nd Sight Lab for a penetration test or security assessment.

Have a Cybersecurity or Cloud Security Question? Ask Teri Radichel by scheduling a call with IANS Research.

Cybersecurity & Cloud Security Resources by Teri Radichel: Cybersecurity and Cloud security classes, articles, white papers, presentations, and podcasts

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Teri Radichel

Teri Radichel

Cloud Security Training and Penetration Testing | GSE, GSEC, GCIH, GCIA, GCPM, GCCC, GREM, GPEN, GXPN | AWS Hero | Infragard | IANS Faculty | 2ndSightLab.com