Home > Software engineering >  How to get value of "string"?
How to get value of "string"?

Time:01-15

Given strings like:

"hello"
'hello'

I want to remove only first and last char if:

  1. They are the same
  2. They are " or '

I.e., given 'hello' I'm expecting hello. Given 'hello" I'm not expecting it to change.

I was able to do this by reading first char and last char, validating they are the same validating they are equal to ' or " and validating it's not the the same index for char (because I don't want this: ' to end up as the empty string). With all edge cases checking I ended with 10s of lines.

What's your approach to solve this?

In simple words, Given a string in Python format I want to return its data and if it's not valid to keep it as is.

CodePudding user response:

Sounds like a job for regular expressions with groups:

import re
re.sub(r'^([\'"])(.*)(\1)$', r'\2', s)

Which reads as:

  • ^ - match the beginning of the string
  • (['"]) - either single or double quote (group 1)
  • (.*) any (possibly, empty) sequence of characters in between (group 2)
  • (\1) - the same character as in group 1
  • $ - end of the string

If the string matches the pattern above, replace it with the content of the group 2.

For example:

>>> s = re.sub(r'^([\'"])(.*)(\1)$', r'\2', "'hello'")
>>> print(s)
hello

An alternative way could be with ast.literal_eval(), but it won't handle non-matching quotes.

CodePudding user response:

I would use str.endswith and str.startswith, although it still gets a bit long:

def readstring(string):
    if len(string)>1 and (string.startswith('"') and string.endswith('"') or string.startswith("'") and string.endswith("'")):
        return string[1:-1]
    return string

CodePudding user response:

you could do this Giant block of chunk compressed:

new = string.replace(string[-1],'').replace(string[0],'') if string[0] == string[-1] or string.startswith('"') and string.startswith("'") and string[0] != string[1] else ([string.replace(x,'')if(x==string[0]or x==string[-1]) and (x=='"'or x=='"') else ... for x in string])
while Ellipsis in new:new.remove(Ellipsis)
new=''.join(new)
  •  Tags:  
  • Related