Quantcast
Channel: How to replace a string conditionally in Python? - Stack Overflow
Browsing index pages (5 articles)
↧

How to replace a string conditionally in Python?

I need to look for a substring in a string and if I don't find it, perform an action. So I do this, and it works fine:if 'AAA' not in myString: print "AAA is not in myString"What I need, however, is to...

View Article


Answer by Christian Dean for How to replace a string conditionally in Python?

I don't want my script to notice any presence of "XAAA" when it does if 'AAA' not in myString.You can first remove any occurrences of the sub-string XAAA from myString, and then test if AAA is not a...

View Article


Answer by Gribouillis for How to replace a string conditionally in Python?

I like re module solutionsif not re.search(r'(?<!X)AAA', myString): ...

View Article

Answer by Anthony Pham for How to replace a string conditionally in Python?

There are two ways.You can remove XAAA from the string then compare:if 'AAA' not in myString.replace('XAAA', ''): # do somethingOr you can check for it first then compare:if 'XAAA' not in myString: if...

View Article

Answer by Mike Robinson for How to replace a string conditionally in Python?

Regular expressions might serve you extremely-well here.For instance, you could match the string against the so-called "regex,"/[^X]?AAA/.This regex tests for the existence of any string AAA which is...

View Article

Browsing index pages (5 articles)


Latest Images