如何运用PYTHON里字符串的count方法
工具/原料
PYTHON
方法/步骤
1、打开JYPTERNOTEBOOK,新建一个空白的PY文档。
2、abc="mynameisTomandiliketobeawinner."首先我们定义一个变量,里面是字符串,尽量长一点,可以做一个测试。
3、print(abc.count("i"))print(abc.count("a"))print(abc.count("e"))用count就可以数出该字母出现的次数,一共是多少次。
4、print(abc.count(&孥恶膈茯quot;i",2,10))print(abc.count("a"稆糨孝汶;,2,10))print(abc.count("e",2,10))当然可以指定范围,从哪里开始,从哪里结束。
5、print(abc.count("i",2))print(abc.count("a",2))print(abc.count("e",2))如果不设定结束点的话,那么就会直接数到最后。
6、print(abc.count("i",0,15))print(abc.count("a",0,15))print(abc.count("e",0,15))如果起始点为0的话,就是从开始数起来。
7、print(abc.count(&孥恶膈茯quot;i",,15))print(abc.count("a"荑樊综鲶,,15))print(abc.count("e",,15))但是如果有结束点,没有起始点的话就会出现错误,不能为空集。