last character on a line is a backslash in python

When the last character in a line is a backslash in a Python program, what does that mean?

Answer.  The backslash is simply used to enable a carriage return like capability when writing your code, as in the example below.

 

print "Hello \ 

         World!"

would provide the same result as 

print "Hello World!"

 

For example, you may have a string of text that is too large to fit in one line such as a really long sentence. While the long sentences' appearance may be suitable for your program output, it may not be easy to work within your IDE.  So like the example above, you use the single backslash at the end of the line to

allow you to go onto the next line and continue typing your code.

 

You will also find the backslash used in other instances of a Python program such as an escape character.  This will be within the string however, as opposed to the end of it.

 

Example:

 

str_ex = "\"Bonjour mes amies.\", she said"

print str_ex

 

The backslash is used to escape and display the double quotes surrounding Bonjour mes amies.  That means good day my friends in French.

 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <strike> <caption>

More information about formatting options