Need help in understanding the below python string extraction

Viewed 56

I read the below code about string extraction in python

text[text.find("insert"):(len(text),text.find("Configure"))[text.find("Configure")>1]]

and I am not able to understand this part

(len(text),text.find("Configure"))[text.find("Configure")>1]

How does this work? Can some one help me?

4 Answers

There are several pieces of code involved in this one line:

I am considering variable text is of str type

  1. (len(text),text.find("Configure")) This code will return a tuple of int, example: (100, 10)
  2. [text.find("Configure")>1] This code will return a list of bool either [True] or [False]
  3. Now combining both code blocks above, if index of "Configure" is greater than 1 then it will be True i.e. 1 so element in tuple at index 1 will be taken i.e. 10, else condition will be False i.e. 0 so element at index 0 will be taken i.e. 100 (from the example tuple defined above)
(len(text),text.find("Configure"))[text.find("Configure")>1]
  1. The whole line boils down to:
text[text.find("insert"):100]

or

text[text.find("insert"):10]

and this is basic string slicing in Python

It is slicing operation. Please see the broken down parts of this slice. Basically it is returning the slice of text from 'Insert' to 'Configure' provided 'Configure' comes after 'Insert'. If not then it returns entire string after 'Insert'. If 'Configure' comes before 'Insert' you get an empty string.

I do not understand the utility of writing this complex expression unless you can tell us about the code where you saw this. I advocate for not using this type of code.

text[
text.find("insert") #start index
:
(len(text),text.find("Configure"))  [text.find("Configure")>1]  # boolean behaving as subscript for entire string or till configure
]

(len(text),text.find("Configure") this part is creating a tuple of the length of the string and the index of the first instance of the word "Configure".

So if text = '0000Configure000' the tuple would become (16, 4).

Now, the other part [text.find("Configure")>1] the condition is checking whether the index of the first instance of the word "Configure" is greater than 1.

Since in my example the index is 4 the condition becomes true and since it's inside a list [text.find("Configure")>1] becomes [True]

Now,

(len(text),text.find("Configure"))[text.find("Configure")>1]

becomes :

(16, 4)[True]

And in python True is considered as 1 and False is considered as 0

so the code above can be written as

(16, 4)[1]

Which becomes the value 4

All of the answer with comments.

text1 = "sdgasdgasd insert blaBLAbla Configure"
text2 = "sdgasdgasd insert blaBLAbla Configur"

# For text1
# OUTPUT : (26, 17)
print((len(text1),text1.find("Configure")))

# OUTPUT : True also '1'
print(text1.find("Configure")>1)

# OUTPUT : 17 because '(26, 17)' is a list and '1' also second element is 17
print((len(text1),text1.find("Configure"))[text1.find("Configure")>1])

# from 'insert' until 'Configure' but
# if 'Configure' word in text1, 'Configure' not in print with string
# OUTPUT : 'insert blaBLAbla'
print(text1[text1.find("insert"):(len(text1),text1.find("Configure"))[text1.find("Configure")>1]])

# For text2
# OUTPUT : (36, -1)
print((len(text2),text2.find("Configure")))

# OUTPUT : False also '0'
print(text2.find("Configure")>1)

# OUTPUT : 36 because '(36, -1)' is a list and '0' also first element is 36
print((len(text2),text2.find("Configure"))[text2.find("Configure")>1])

# from 'insert' until 'Configure' but
# if 'Configur' word in text2, 'Configur' in print with string
# OUTPUT : 'insert blaBLAbla Configur'
print(text2[text2.find("insert"):(len(text2),text2.find("Configure"))[text2.find("Configure")>1]])
Related