简体   繁体   中英

how to slice specific characters in python with constraints?

str="class computer :"
name=str[str.find("class"):str.find(":")]
print name

The above code has to output the result as " computer " for which I get, "class computer" . what might be the mistake?

Once you .find('class') you need to offset that index by the length of the string 'class' itself.

>>> s = 'class computer :'
>>> s[s.find('class')+len('class'):s.find(':')]
' computer '

You could throw on a strip to remove the leading and trailing whitespace

>>> s[s.find("class")+len('class'):s.find(":")].strip()
'computer'

str.find returns the index of the first occurring character match and hence 0 is returned

>>> s.find('class')
0

From the docs

Return the lowest index in the string where substring sub is found

Thus you need to add the length of your find string to get the correct output by using the len function

>>> name=s[s.find("class")+len('class'):s.find(":")]
>>> print name
 computer 

Note - You should not use str as a variable as it shadows the built-in functionality

If you have any other word with class in it your find will fail, you can use a regex with word boundaries to find an exact match for the word class :

import  re

print(re.findall(r"(?<=\bclass\b)\s+\w+\s+",s))

You can see an example of how it will fail:

In [9]: s = "subclass class computer :"

In [10]: s[s.find('class')+len('class'):s.find(':')]
Out[10]: ' class computer '

In [11]: re.findall(r"(?<=\bclass\b)\s+\w+\s+",s)
Out[11]: [' computer ']

If you have more than one class in your string you can use a lookahead with the lookbehind assertion:

 s = "subclass class tv class computer :"
 print(re.findall(r"(?<=\bclass\b)\s+\w+\s+(?=:)",s))

 [' computer ']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM