python字符串find_Python字符串| 带示例的find()方法
python字符串find
String.find()方法 (String.find() Method)
find() is an inbuilt method of python, it is used to check whether a sub-string exists in the string or not. If sub-string exists, the method returns the lowest index of the sub-string, if sub-string does not exist, method return -1.
find()是python的內(nèi)置方法,用于檢查字符串中是否存在子字符串。 如果存在子字符串,則該方法返回子字符串的最低索引,如果子字符串不存在,則方法返回-1。
Note: This method is case sensitive.
注意:此方法區(qū)分大小寫。
Syntax:
句法:
String.find(sub_string[, start_index[, end_index]])Parameters:
參數(shù):
sub_string - a part of the string to be found in the string.
sub_string-要在字符串中找到的字符串的一部分。
start_index - an optional parameter, it defines the starting index from where sub_string should be found.
start_index-可選參數(shù),它定義從中應(yīng)找到sub_string的起始索引。
end_index - an optional parameter, it defines end index of the string. Find method will find the sub_string till this end_index.
end_index-可選參數(shù),它定義字符串的結(jié)束索引。 Find方法將查找sub_string直到此end_index 。
Return value:
返回值:
Returns the lowest index of the sub_string, if it exits in the string.
返回 sub_string 的最低索引 ,如果它存在于字符串中。
Returns -1 if sub_string does not exist in the string.
如果sub_string在字符串中不存在,則返回-1 。
Example:
例:
# string in which we have to find the sub_string str = "Hello world, how are you?"# sub_string to find the given string sub_str = "how"# find by sub_str print (str.find (sub_str))# find by sub_str with slice:start index print (str.find (sub_str, 10))# find by sub_str with slice:start index and slice: end index print (str.find (sub_str, 10, 24))# find a sub_str that does not exist sub_str = "friend" # find by sub_str print (str.find (sub_str))# find a sub_str with different case sub_str = "HOW" # find by sub_str print (str.find (sub_str))Output
輸出量
131313-1-1翻譯自: https://www.includehelp.com/python/string-find-method-with-example.aspx
python字符串find
總結(jié)
以上是生活随笔為你收集整理的python字符串find_Python字符串| 带示例的find()方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分披萨问题_比萨疯狂问题
- 下一篇: Java Hashtable rehas