Python Forum

Full Version: which is better?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
which is better?

0.
    if s[0] == "'" and s[-1] == "'":
1.
    if (s[0],s[-1]) == ("'","'"):
2.
    if (s[0]+s[-1]) == "''":
or any other suggestions?
I would say the first one is the most readable code of the three. I would also consider a regular expression or:

if s.startswith("'") and s.endswith("'")
+1 for ichabod's suggestion
(Aug-08-2017, 01:28 AM)ichabod801 Wrote: [ -> ]I would say the first one is the most readable code of the three. I would also consider a regular expression or:

if s.startswith("'") and s.endswith("'")
i'm going to be removing the quotes.  anything better than:
    if s.startswith("'") and s.endswith("'"):
        s = s[1:-1]
?
FYI, s comes from repr() which will have the quotes if a string is given to it, but those will be awkward for the usage, which is to be part of a filename.
s = "'some'string'"
print s
print s.strip("'")
Output:
'some'string' some'string
Given the additional info I would consider skipping the if part and clearing the quote even if only in begining or only at the end
if there are quotes in the middle, i want to keep them.  i'm removing the quotes that repr() adds to a string, but it might have not been a string.  i should make some variant code for unicode (v2), bytes (v3), and bytearray.