Good practice for python: 'vs'

Possible duplicate:
Single quotes and double quotes in Python

I saw that when I need to work with a string in Python, both of the following syntax are accepted:

mystring1 = "here is my string 1" mystring2 = 'here is my string 2' 

Anyway, is there any difference?

For any reason, is it better to use one solution and not another?

Greetings

+6
source share
7 answers

No no. When a string contains a single quote, it is easier to enclose it in double quotes and vice versa. Other than that, my advice would be to choose a style and stick to it.

Another useful type of string literals are triple quotes, which can span multiple lines:

 s = """string literal... ...continues on second line... ...and ends here""" 

Again, you need to use single or double quotes for this.

Finally, I would like to mention "raw string literals". They are enclosed in r"..." or r'...' and do not allow the analysis of control sequences (for example, \n ) as such. Among other things, the original string literals are very convenient for specifying regular expressions.

Read more about Python string literals here .

+5
source

Although itโ€™s true that there is no difference between the two, I found a lot of the following behavior in the opensource community:

  • " for text to be read (email, feeback, execption, etc.)
  • ' for data text (dict key, function arguments, etc.)
  • triple " for any docstring or text that includes " and '
+3
source

Not. Just a matter of style. Just be consistent.

+1
source

I tend to use " simply because it uses most other programming languages.

So, habit, really.

+1
source

There is no difference.

What could be better. I use "..." for text strings and '...' for characters, as this is consistent with other languages โ€‹โ€‹and can save you a few clicks when transferring to / from another language. For regular expressions and SQL queries, I always use r'''...''' because they often end up with a backslash and both types of quotes.

+1
source

Python is the least code to get the most effect. The shorter the better. And ' , in a way, one dot is shorter, " so I prefer it. :)

+1
source

As everyone noted, they are functionally identical. However, PEP 257 (Docstring Conventions) suggests that you always use the """ around docstrings for consistency purposes only. No one can scream at you or think badly of you if you don't, but it is.

0
source

Source: https://habr.com/ru/post/900281/


All Articles