String Part 2 in Python3


There is a special character called backslash character, it has a special meaning in strings, it's used to escape the character that follows it to give that character special meaning. The backslash character provide us a new line. So, let's see.



Now, as you can see \n has actually caused the cursor to start on the next line. So, for each one of those \n's we added that actually spit onto a separate line each time we did it.


Now, we'll do the same with tab character.



It's used a standard tab function which you’ve probably seen in a word processor before to tab things out which is a way of sort of organizing things in a certain position.
You can also use that backslash to escape special characters such as quote and double quotes, and that’s useful if you’ve got a string containing both characters.
Let me show the example.



So here, if we are using both single quotes and double quotes in a particular string which you may want to use some time. If started off with this string by saying it’s a single quote, but by putting the backslash in front of the quote you want to use, that actually tells Python, to treat it not as a special command to close the string in this case, but to set the string that can actually be printed out and you can do same as with double quotes.
The string that we split over several lines earlier is not easy to read because of the embedded slash and as you can saw Python allows a string to be split over several lines without doing that by using triple quotes, and the way to do that, we type something like this.



Note : Don’t use four quotes in a row instead of three because that doesn’t work to well and you’ll find that Python gets itself into a little bit of a knot with that and if you’re using the three quotes you need to actually have a space before to make sure you haven’t got four quotes in a row.


Comments

Popular posts from this blog

String Slicing and Picking sub-string in Python3

Integers and Float in Python3

Variable in Python3