Summary of Knowledge Points Differentiated by Single Quoted and Double Quoted in python

  • 2021-06-29 11:34:50
  • OfStack

What is the difference between single quotation marks and double quotation marks in python?Here is a detailed introduction for you. 1:

First, the difference between double quotation marks and 3 quotation marks is that the string represented by double quotation marks is usually written on a line.

For example:


s1 = "hello,world"

If you want to write on more than one line, use \ ("hyphen"), for example:


s2 = "hello,\ world"

s2 is the same as s1.If you use three double quotes, you can write directly as follows:


s3 = """hello, world, hahaha."""

Recommendations: Python Video Tutorial

So s3 is actually "hello,nworld,nhahaha." Note "n." So if you have manyns in your string and you don't want to usen in your string, you can use three double quotes.You can also add comments to the string using three double quotes, as follows:


s3 = """hello, #hoho, this is hello,  stay 3 There can be comments in double quoted strings  world,  #hoho, this is world hahaha."""

This is the difference between three double quotation marks and one double quotation mark for string. The difference between three double quotation marks and one single quotation mark is the same. In fact, there is a reason python supports single quotation marks. Let me compare the difference between one single quotation mark and one double quotation mark.When I use single quotation marks to represent a string, if I want to represent Let's go, I have to do this: s4 ='Let\'s go', note that there is one', and the string is'so use the escape character (, you know the escape character),If you have a bunch of escape characters in your string that must look uncomfortable, python also solves this problem very well, as follows:


s5 = "Let's go"

At this point, we see that python knows you are using "to represent a string, so it is not easy for python to treat the single quotation mark in the string as a normal character.For double quotation marks, the same is true. Here is an example:


s6 = 'I realy like "python"!'

This is why both single and double quotes can represent strings.


Related articles: