Mẹo Hướng dẫn Should i use single or double quotes in python? Đầy đủ Chi Tiết

You đang tìm kiếm từ khóa Should i use single or double quotes in python? Đầy đủ được Cập Nhật vào lúc : 2022-09-30 17:00:00 . Với phương châm chia sẻ Mẹo về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Mình lý giải và hướng dẫn lại nha.

Mẹo Hướng dẫn Should i use single or double quotes in python? 2022

Quý khách đang tìm kiếm từ khóa Should i use single or double quotes in python? được Update vào lúc : 2022-09-30 17:00:21 . Với phương châm chia sẻ Mẹo Hướng dẫn trong nội dung nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.

According to the documentation, they’re pretty much interchangeable. Is there a stylistic reason to use one over the other?

Nội dung chính

    Should I use single or double quotes?
    Do quotation marks matter in Python?

asked Sep 11, 2008 8:18

readonlyreadonly

332k107 gold badges203 silver badges204 bronze

badges

0

I like to use double quotes around strings that are used for interpolation or that are natural language messages,

and single quotes for small symbol-like strings, but will break the rules if the strings contain quotes, or if I forget. I use triple double quotes for docstrings and raw string literals for regular expressions even if they aren’t needed.

For example:

LIGHT_MESSAGES =

‘English’: “There are %(number_of_lights)s lights.”,

‘Pirate’: “Arr! Thar be %(number_of_lights)s lights.”

def lights_message(language, number_of_lights):

“””Return a language-appropriate string reporting the light count.”””

return LIGHT_MESSAGES[language] % locals()

def is_pirate(message):

“””Return True if the given message sounds piratical.”””

return re.search(r”(?i)(arr|avast|yohoho)!”, message) is not None

answered Sep 11, 2008 10:06

Will HarrisWill Harris

21.5k11 gold badges64 silver badges63

bronze badges

7

Quoting the official docs

://docs.python.org/2.0/ref/strings.html:

In plain English: String literals can be enclosed in matching single quotes (‘) or double quotes (“).

So there is no difference. Instead, people will tell you to choose whichever style that matches the context, and to be consistent. And I would agree – adding that it is pointless to try to come up with

“conventions” for this sort of thing because you’ll only end up confusing any newcomers.

peko

11.2k4 gold badges32 silver

badges47 bronze badges

answered Sep 27, 2008 18:07

1

I used to prefer ‘, especially for ”’docstrings”’, as I find “””this creates some fluff”””. Also, ‘ can be typed without the Shift key on my Swiss German keyboard.

I have since changed to using triple quotes for “””docstrings”””, to conform to

PEP 257.

answered Sep 11, 2008 8:55

Daren ThomasDaren Thomas

66k40 gold badges149 silver badges198 bronze badges

12

I’m

with Will:

    Double quotes for text
    Single quotes for anything that behaves like an identifier
    Double quoted raw string literals for regexps
    Tripled double quotes for docstrings

I’ll stick with that even if it means a lot of escaping.

I get the most value out of single quoted identifiers standing out because of the quotes. The rest of the practices are there just to give those single quoted identifiers some standing room.

answered Sep 11, 2008 10:21

Garth

KiddGarth Kidd

7,0725 gold badges35 silver badges36 bronze badges

If the

string you have contains one, then you should use the other. For example, “You’re able to do this”, or ‘He said “Hi!”‘. Other than that, you should simply be as consistent as you can (within a module, within a package, within a project, within an organisation).

If your code is going to be read by people who work with C/C++ (or if you switch between those languages and Python), then using ” for single-character strings, and “” for longer strings might help ease the transition. (Likewise for following

other languages where they are not interchangeable).

The Python code I’ve seen in the wild tends to favour ” over ‘, but only slightly. The one exception is that “””these””” are much more common than ”’these”’, from what I have seen.

Tadeck

128k28 gold badges149 silver badges197 bronze badges

answered Sep 11, 2008 8:33

Tony MeyerTony Meyer

9,8476 gold badges44 silver badges47 bronze

badges

Triple quoted comments are an interesting subtopic of this question. PEP 257 specifies triple quotes for doc

strings. I did a quick check using Google Code Search and found that triple double quotes in Python are about 10x as popular as triple single quotes — 1.3M vs 131K occurrences in the code Google indexes. So in the multi line case your code is probably going to be

more familiar to people if it uses triple double quotes.

answered Jul 30, 2009 20:35

jblocksomjblocksom

13.6k5 gold badges34 silver badges30 bronze badges

0

“If you’re going to use apostrophes,

^

you’ll definitely want to use double quotes”.

^

For that simple reason, I always use double quotes on the outside. Always

Speaking of fluff, what good is streamlining your string literals with ‘ if you’re going to have to use escape characters to represent apostrophes? Does it offend coders to read novels? I can’t imagine how painful high school English class was for you!

answered Sep 22, 2011 12:35

yurisichyurisich

6,8435 gold badges41 silver badges62 bronze badges

2

Python uses

quotes something like this:

mystringliteral1=”this is a string with ‘quotes’”

mystringliteral2=’this is a string with “quotes”‘

mystringliteral3=”””this is a string with “quotes” and more ‘quotes’”””

mystringliteral4=”’this is a string with ‘quotes’ and more “quotes””’

mystringliteral5=’this is a string with “quotes”‘

mystringliteral6=’this is a string with 42quotes 42′

mystringliteral6=’this is a string with 47quotes 47’

print mystringliteral1

print mystringliteral2

print mystringliteral3

print mystringliteral4

print mystringliteral5

print mystringliteral6

Which gives the following output:

this is a string with ‘quotes’

this is a string with “quotes”

this is a string with “quotes” and more ‘quotes’

this is a string with ‘quotes’ and more “quotes”

this is a string with “quotes”

this is a string with ‘quotes’

Tadeck

128k28 gold badges149 silver badges197 bronze badges

answered Jan 23, 2011 21:45

MichaelMichael

811 silver badge1 bronze badge

3

I use double quotes in general, but not for any specific reason – Probably just out of habit from Java.

I guess you’re also more likely to want apostrophes in an inline literal string than you are to want double quotes.

answered Sep 11, 2008 8:40

Matt SheppardMatt Sheppard

115k46 gold badges108 silver badges129 bronze badges

Personally I stick with one or the other. It doesn’t matter. And providing your

own meaning to either quote is just to confuse other people when you collaborate.

answered Sep 28, 2008 3:35

schwaschwa

11.9k14 gold badges42 silver badges54 bronze badges

It’s probably a stylistic preference more than anything. I just checked PEP 8 and didn’t see any mention of single versus double quotes.

I prefer single quotes because its only one keystroke instead of two. That is, I don’t have to mash the shift key to make single quote.

answered Sep 19, 2008 19:34

MikeMike

3,5933 gold

badges20 silver badges12 bronze badges

1

In Perl you want to use single quotes when you have a

string which doesn’t need to interpolate variables or escaped characters like n, t, r, etc.

PHP makes the same distinction as Perl: content in single quotes will not be interpreted (not even n will be converted), as opposed to double quotes which can contain variables to have their value printed out.

Python does not, I’m afraid. Technically seen, there is no $ token (or the like) to separate a name/text from a variable in Python. Both features make Python more

readable, less confusing, after all. Single and double quotes can be used interchangeably in Python.

answered Jul 5, 2010 12:51

PeterinoPeterino

13.4k3 gold badges26 silver badges28 bronze badges

2

I chose to use double quotes because they are easier to see.

answered Mar 10, 2009 6:25

Andrew DalkeAndrew Dalke

14.5k3 gold badges38 silver badges53

bronze badges

I just use whatever strikes my fancy the time; it’s convenient to be able to switch between the two a whim!

Of course, when quoting quote characetrs, switching

between the two might not be so whimsical after all…

answered Jul 22, 2009 19:50

Your team’s taste or your project’s coding guidelines.

If

you are in a multilanguage environment, you might wish to encourage the use of the same type of quotes for strings that the other language uses, for instance. Else, I personally like best the look of ‘

answered Sep 11, 2008 8:25

Vinko Vrsalovic♦Vinko Vrsalovic

321k52 gold badges329 silver badges368 bronze badges

None as far as I know. Although if you look some code, ” ” is commonly used for strings of text (I guess ‘ is more common inside text than “), and ‘ ‘ appears in hashkeys and things like that.

answered Sep 11, 2008 8:28

Mario FMario F

42.9k6 gold badges36 silver badges38 bronze badges

I aim to minimize both pixels and surprise. I typically

prefer ‘ in order to minimize pixels, but ” instead if the string has an apostrophe, again to minimize pixels. For a docstring, however, I prefer “”” over ”’ because the latter is non-standard, uncommon, and therefore surprising. If now I have a bunch of strings where I used ” per the above logic, but also one that can get away with a ‘, I may still use ” in it to preserve consistency, only to minimize surprise.

Perhaps it helps to think of the

px minimization philosophy in the following way. Would you rather that English characters looked like A B C or AA BB CC? The latter choice wastes 50% of the non-empty pixels.

answered Apr 16, 2013 22:32

AsclepiusAsclepius

52.1k15 gold badges150 silver badges131 bronze badges

I use double quotes because I have been doing so for years in most languages (C++, Java, VB…) except Bash, because I also use double quotes in normal text and because I’m using a (modified) non-English keyboard where both characters require the shift key.

answered Jul 5, 2010 13:06

PhilippPhilipp

46.5k12 gold badges83 silver badges108 bronze badges

‘ = “

/ = =

example :

f = open(‘c:word.txt’, ‘r’)

f = open(“c:word.txt”, “r”)

f = open(“c:/word.txt”, “r”)

f = open(“c:word.txt”, “r”)

Results are the same

=>> no, they’re not the same. A single backslash will escape characters. You just happen to luck out in that example because k and w aren’t valid escapes like t or n or or “

If you want to use single backslashes (and have them interpreted as such), then you need to use a “raw” string. You can do this by putting an ‘r’ in front of the string

im_raw = r’c:temp.txt’

non_raw = ‘c:temp.txt’

another_way = ‘c:/temp.txt’

As far

as paths in Windows are concerned, forward slashes are interpreted the same way. Clearly the string itself is different though. I wouldn’t guarantee that they’re handled this way on an external device though.

Tadeck

128k28 gold badges149 silver badges197 bronze badges

answered Jan 19, 2010 4:30

kn3lkn3l

19.1k28 gold badges87 silver badges123 bronze badges

1

Should I use single or double quotes?

In US English, you must use double quotation marks. Single quotation marks are used for quotes within quotes. In UK English, it’s most common to use single quotation marks, with double quotation marks for quotes within quotes, although the other way around is acceptable too.

Do quotation marks matter in Python?

In Python, you can create a string with double quotation marks or single quotation marks. There is no difference. When using quotation marks inside a string, make sure to escape them not to cause syntax errors. A triple quotation mark in the string is used as a documentation string.

Tải thêm tài liệu liên quan đến nội dung nội dung bài viết Should i use single or double quotes in python?

programming

python

Quote Python

Convert quotes python

Prettier single quotes

Python Triple quote

Reply
9
0
Chia sẻ

Share Link Down Should i use single or double quotes in python? miễn phí

Bạn vừa Read Post Với Một số hướng dẫn một cách rõ ràng hơn về Video Should i use single or double quotes in python? tiên tiến và phát triển và tăng trưởng nhất ShareLink Tải Should i use single or double quotes in python? Free.

Hỏi đáp vướng mắc về Should i use single or double quotes in python?

Nếu sau khi đọc nội dung nội dung bài viết Should i use single or double quotes in python? vẫn chưa hiểu thì hoàn toàn hoàn toàn có thể lại Comments ở cuối bài để Tác giả lý giải và hướng dẫn lại nha

#single #double #quotes #python

Related posts:

4534

Video Should i use single or double quotes in python? Đầy đủ ?

Bạn vừa Read tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Review Should i use single or double quotes in python? Đầy đủ tiên tiến và phát triển nhất

Share Link Cập nhật Should i use single or double quotes in python? Đầy đủ miễn phí

Pro đang tìm một số trong những Chia SẻLink Tải Should i use single or double quotes in python? Đầy đủ Free.

Thảo Luận vướng mắc về Should i use single or double quotes in python? Đầy đủ

Nếu sau khi đọc nội dung bài viết Should i use single or double quotes in python? Đầy đủ vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Admin lý giải và hướng dẫn lại nha
#single #double #quotes #python #Đầy #đủ