Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is better?
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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("'")
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
+1 for ichabod's suggestion
Recommended Tutorials:
Reply
#4
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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
Reply
#6
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020