Python_API_String Services_string.count

来源:互联网 发布:怎样设置淘宝地理位置 编辑:程序博客网 时间:2024/06/08 05:52

API文档:

string.count(s, sub[, start[, end]])
Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.

翻译文档:

           s:被匹配的字符串

           sub:匹配的字符串

           start:字符串的开始位置

           end:字符串结束为止

          这个方法和string.find的搜索方法是用上基本一致,但是返回的是子字符串的出现次数,没有找到则返回0

例子:

#! /usr/bin/env python
#coding=utf-8

import string

line1="a <nd abc nd > "

print string.count(line1,'nd')
print string.count(line1,'safsaf')

输出:

2
0




原创粉丝点击