*Memos:
- My post explains a string.
count() can count a set of zero or more characters of a string as shown below:
*Memos:
- The 1st argument is
sub
(Required-Type:str
). *Don't usesub=
. - The 2nd argument is
start
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
,0
is set. - Don't use
start
.
- If it's not set or
- The 3rd argument is
end
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
, the length+ 1
is set. - Don't use
end
.
- If it's not set or
- If
sub
is an empty string and ifstart
andend
aren't set, the length+ 1
is returned.
v = 'hello world' print(v.count('l')) print(v.count('l', None, None)) # 3 print(v.count('o')) # 2 print(v.count('d')) # 1 print(v.count('l', 3)) print(v.count('l', 3, 11)) # 2 print(v.count('l', 3, 9)) # 1 print(v.count('L')) print(v.count('a')) # 0 print(v.count('ll')) print(v.count('lo')) print(v.count('wo')) # 1 print(v.count('')) print(v.count('', 0, 11)) # 12 print(v.count('', 0, 4)) print(v.count('', 3, 7)) print(v.count('', 7, 11)) # 5
v = '' print(v.count('')) # 1
startswith() can check if it's the prefix of a string as shown below:
*Memos:
- The 1st argument is
prefix
(Required-Type:str
): *Memos:- If it's an empty string,
True
is returned. - Don't use
prefix=
.
- If it's an empty string,
- The 2nd argument is
start
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
,0
is set. - Don't use
start
.
- If it's not set or
- The 3rd argument is
end
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
, the length+ 1
is set. - Don't use
end
.
- If it's not set or
v = 'hello world' print(v.startswith('he')) # True print(v.startswith('he', None, None)) # True print(v.startswith('HE')) # False print(v.startswith('wo')) # False print(v.startswith(('he', 'wo'))) # True print(v.startswith(('wo', 'he'))) # True print(v.startswith(('wo', 'lo'))) # False print(v.startswith('he', 0)) # True print(v.startswith('he', 1)) # False print(v.startswith('he', 0, 1)) # False print(v.startswith('he', 0, 2)) # True print(v.startswith('wo', 6, 7)) # False print(v.startswith('wo', 6, 8)) # True print(v.startswith('')) # True
v = '' print(v.startswith('')) # True
endswith() can check if it's the suffix of a string as shown below:
*Memos:
- The 1st argument is
suffix
(Required-Type:str
): *Memos:- If it's an empty string,
True
is returned. - Don't use
suffix=
.
- If it's an empty string,
- The 2nd argument is
start
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
,0
is set. - Don't use
start
.
- If it's not set or
- The 3rd argument is
end
(Optional-Type:int
orNoneType
): *Memos:- If it's not set or
None
, the length+ 1
is set. - Don't use
end
.
- If it's not set or
v = 'hello world' print(v.endswith('ld')) # True print(v.endswith('ld', None, None)) # True print(v.endswith('LD')) # False print(v.endswith('lo')) # False print(v.endswith(('ld', 'lo'))) # True print(v.endswith(('lo', 'ld'))) # True print(v.endswith(('lo', 'wo'))) # False print(v.endswith('ld', 9)) # True print(v.endswith('ld', 10)) # False print(v.endswith('ld', 9, 10)) # False print(v.endswith('ld', 9, 11)) # True print(v.endswith('lo', 3, 4)) # False print(v.endswith('lo', 3, 5)) # True print(v.endswith('')) # True
v = '' print(v.endswith('')) # True
Top comments (0)