Reputation: 798
I have a two strings
code_one = "222abc"
code_two = "2abc"
Is there a way I can determine that strings begin with "2" repeating any number of times?
Upvotes: 0
Views: 80
Reputation: 87
Assuming tests would include characters other than '2', perhaps grab character [0] and compare to [1] and if equal then lstrip on that character as shown to get a count (if needed )
Upvotes: 0
Reputation: 41168
You can simply use lstrip()
and compare the lengths:
>>> code_one = "222abc"
>>> len(code_one) - len(code_one.lstrip("2"))
3
Or if you just want to check the string starts with some characters:
>>> code_one.startswith("222")
True
Upvotes: 8