*Memos:
- My post explains a string.
find() can find the 1st substring of a string, searching from the left to the right to return the index (without error even if the substring isn't found) as shown below:
*Memos:
- The 1st argument is
sub
(Required-Type:str
): *Memos:- It's the substring of zero or more characters.
- Don't use
sub=
.
- 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
-
-1
is returned if the substring isn't found.
v = 'grapes peach' print(v.find('pe')) # 3 print(v.find('pe', None, None)) # 3 print(v.find('PE')) # -1 print(v.find('pe', 3)) # 3 print(v.find('pe', 4)) # 7 print(v.find('pe', 7)) # 7 print(v.find('pe', 8)) # -1 print(v.find('pe', 0, 4)) # -1 print(v.find('pe', 0, 5)) # 3 print(v.find('pe', 7, 12)) # 7 print(v.find('pe', 8, 12)) # -1 print(v.find('')) # 0 print(v.find('g', 0, 1)) # 0 print(v.find('h', 11, 12)) # 1 print(v.find('abc')) # -1
v = '' print(v.find('')) # 0
rfind() can find the 1st substring of a string, searching from the right to the left to return the index (without error even if the substring isn't found) as shown below:
*Memos:
- The 1st argument is
sub
(Required-Type:str
): *Memos:- It's the substring of zero or more characters.
- Don't use
sub=
.
- 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
-
-1
is returned if the substring isn't found.
v = 'grapes peach' print(v.rfind('pe')) # 7 print(v.rfind('pe', None, None)) # 7 print(v.rfind('PE')) # -1 print(v.rfind('pe', 3)) # 7 print(v.rfind('pe', 4)) # 7 print(v.rfind('pe', 7)) # 7 print(v.rfind('pe', 8)) # -1 print(v.rfind('pe', 0, 4)) # -1 print(v.rfind('pe', 0, 5)) # 3 print(v.rfind('pe', 7, 12)) # 7 print(v.rfind('pe', 8, 12)) # -1 print(v.rfind('')) # 12 print(v.rfind('g', 0, 1)) # 0 print(v.rfind('h', 11, 12)) # 11 print(v.rfind('abc')) # -1
v = '' print(v.rfind('')) # 0
index() can find the 1st substring of a string, searching from the left to the right to return the index (with error if the substring isn't found) as shown below:
*Memos:
- The 1st argument is
sub
(Required-Type:str
): *Memos:- It's the substring of zero or more characters.
- Don't use
sub=
.
- 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
- Error occurs if the substring isn't found.
v = 'grapes peach' print(v.index('pe')) # 3 print(v.index('pe', None, None)) # 3 print(v.index('pe', 3)) # 3 print(v.index('pe', 4)) # 7 print(v.index('pe', 7)) # 7 print(v.index('pe', 0, 5)) # 3 print(v.index('pe', 7, 12)) # 7 print(v.index('')) # 0 print(v.index('g', 0, 1)) # 0 print(v.index('h', 11, 12)) # 11 print(v.index('PE')) print(v.index('pe', 8)) print(v.index('pe', 0, 4)) print(v.index('pe', 8, 12)) print(v.index('abc')) # ValueError: substring not found
v = '' print(v.index('')) # 0
rindex() can find the 1st substring of a string, searching from the right to the left to return the index (with error if the substring isn't found) as shown below:
*Memos:
- The 1st argument is
sub
(Required-Type:str
): *Memos:- It's the substring of zero or more characters.
- Don't use
sub=
.
- 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
- Error occurs if the substring isn't found.
v = 'grapes peach' print(v.rindex('pe')) # 7 print(v.rindex('pe', None, None)) # 7 print(v.rindex('pe', 3)) # 7 print(v.rindex('pe', 7)) # 7 print(v.rindex('pe', 0, 5)) # 3 print(v.rindex('pe', 7, 12)) # 7 print(v.rindex('')) # 12 print(v.rindex('g', 0, 1)) # 0 print(v.rindex('h', 11, 12)) # 11 print(v.rindex('PE')) print(v.rindex('pe', 8)) print(v.rindex('pe', 0, 4)) print(v.rindex('pe', 8, 12)) print(v.rindex('abc')) # ValueError: substring not found
v = '' print(v.rindex('')) # 0
Top comments (0)