Reputation: 559
I'm trying to figure out how to select a specific character from a string. I know you can use the [0:0]
and [0:-0]
syntax etc... However I'm trying to do something different.
If a user enters "Hello, my name is [lol] bob [not[really]] john[son]"
or enters "[[[[]]][[][][]"
I'm trying to count how many square brackets have been typed and either left or right.
Thanks a lot guys for the instant response, a lot of help!
Upvotes: 1
Views: 576
Reputation: 39405
May be there have a better way, but it will do:
input = 'Hello, my name is [lol] bob [not[really]] john[son]'
print len(re.findall("\[|\]", input))
Upvotes: 0
Reputation: 29804
You can use str.count
method if you only need to count them:
>>>s = "Hello, my name is [lol] bob [not[really]] john[son]"
>>>s.count('[') + s.count(']')
8
Upvotes: 3