tMC
tMC

Reputation: 19355

Simple string match

I want to do a simple string match, searching from the -front- of the string. Are there easier ways to do this with maybe a builtin? (re seems like overkill)

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    if ipadr[0:len(matchIP)] == matchIP: return True
    return False

Upvotes: 2

Views: 1653

Answers (3)

Andrey Sboev
Andrey Sboev

Reputation: 7682

'10.20.30.40'.startswith('10.20.')
>>>True

Upvotes: 1

Michael Lorton
Michael Lorton

Reputation: 44436

>>> 'abcde'.startswith('abc')
True

Upvotes: 1

Sven Marnach
Sven Marnach

Reputation: 602715

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    return ipadr.startswith(matchIP)

Upvotes: 7

Related Questions