Quotation Marks and Special Characters in Python

Skillyfy provides best Python Training in DELHI NCR. We have a team of experienced Python professionals and trainers who have vested many years in this ever-growing IT industry.

Through our best Python Training course in Delhi you’ll learn all of the Python fundamentals. Each chapter ends with exercises, allowing you to put your new learned skills into practical use for a better understanding. Knowing Python opens a great deal of doors for you as a developer. This blog is about Quotation marks and Special characters in Python course in Delhi NCR.

Quotation Marks and Special Characters   

Strings can be created with single quotes or double quotes. There is no difference between the two.

Escaping Characters

To output a string that contains a single quote (e.g., Where’d you get the coconuts?), enclose the string in double quotes:

phrase = “Where’d you get the coconuts?”

Likewise, to output a string that contains a double quote (e.g., The soldier asked, “Are you suggesting coconuts migrate?”), enclose the string in single quotes:

phrase = ‘The soldier asked, “Are you suggesting coconuts migrate?”‘

Sometimes you will want to output single quotes within a string denoted by single quotes or double quotes within a string denoted by double quotes. In such cases, you will need to escape the quotation marks using a backslash (\), like this:

phrase = “The soldier said, \”You’ve got two empty halves of a co »»

conut and you’re bangin’ ’em together.\””

#or

phrase = ‘The soldier said, “You\’ve got two empty halves of a co »»

conut and you\’re bangin\’ \’em together.”‘

Special Characters

The backslash can also be used to escape characters that have special meaning, such as the backslash itself:

phrase = “Use an extra backslash to output a backslash: \\”

Two other common special characters are the linefeed (\n) and horizontal tab (\t).

Escape Sequences

Escape Sequence Meaning
\’ Single quote
\” Double quote
\\ Backslash
\n Linefeed
\t Horizontal tab

Triple Quotes

Triple quotes are used to create multi-line strings. You can use three double quotes16 as shown in the example below:

Code Sample

Text Box: strings/Demos/triple_quotes.py

1.   print("""----------------
2.   LUMBERJACK SONG
3.
4.   I'm a lumberjack
5.   And I'm O.K.
6.   I sleep all night
7.   And I work all day.
8.   ----------------""")
9.
10.
11.  print('''----------------
12.  LUMBERJACK SONG
13.
14.  I'm a lumberjack
15.  And I'm O.K.
16.  I sleep all night
17.  And I work all day.
18.  ----------------''')

Code Explanation

The above code will render the following:

Notice that quotation marks can be included within triple-quoted strings without being escaped with a backslash.

In some cases when using triple quotes, you may want to break up your code with a linefeed without having that linefeed show up in your output. You can escape the actual linefeed with a backslash as shown below:

Code Sample

Text Box: strings/Demos/triple_quotes_linefeed_escaped.py
1.   print("""We're knights of the Round Table, \
2.   we dance whene'er we're able.
3.   We do routines and chorus scenes \
4.   with footwork impeccable,
5.   We dine well here in Camelot, \
6.   we eat ham and jam and Spam a lot.""")

Code Explanation

The above code will render the following:

Notice that the backslashes at the end of lines 1, 3, and 5 prevent line breaks in the output.

This article is contributed by Skillyfy. Skillyfy provides best Python Training institute in DELHI NCR with 100% complete practical demonstrations.

Please call on 7827814720 or email info@skillyfy.com if you want to take more

information about the topic discussed above or any other courses.

Happy Learning !!

Source Url: https://javatrainingindelhi.weebly.com/blog/functional-lambdas-in-java-8

Published by Skillyfy

Skillyfy is known for its IT training, it provides best programming courses for Java, Python, Dot Net, C/C++, etc. It also provides Summer Internship and also security courses like Ethical Hacking and all this at a very affordable price.

Leave a comment

Design a site like this with WordPress.com
Get started