[Python] Check str.count('') and len(str) for empty strings

Asked 2 years ago, Updated 2 years ago, 28 views

a=''
b='a'
print(len(a))
print(a.count(''))
print('\n')
print(len(b))
print(b.count(''))

If you squeeze the cord like this,

0
1


1
2

The results come out like this, but I don't understand how to count in count.

code

2022-09-22 17:59

1 Answers

It's a regular expression.

In [1]: import re                                                               

In [2]: re.findall('', 'a')                                                     
Out[2]: ['', '']


2022-09-22 17:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.