养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

如何运用PYTHON里字符串的count方法

时间:2024-09-20 03:49:25

如何运用PYTHON里字符串的count方法

工具/原料

PYTHON

方法/步骤

1、打开JYPTERNOTEBOOK,新建一个空白的PY文档。

如何运用PYTHON里字符串的count方法

2、abc="mynameisTomandiliketobeawinner."首先我们定义一个变量,里面是字符串,尽量长一点,可以做一个测试。

如何运用PYTHON里字符串的count方法

3、print(abc.count("i"))print(abc.count("a"))print(abc.count("e"))用count就可以数出该字母出现的次数,一共是多少次。

如何运用PYTHON里字符串的count方法

4、print(abc.count(&孥恶膈茯quot;i",2,10))print(abc.count("a"稆糨孝汶;,2,10))print(abc.count("e",2,10))当然可以指定范围,从哪里开始,从哪里结束。

如何运用PYTHON里字符串的count方法

5、print(abc.count("i",2))print(abc.count("a",2))print(abc.count("e",2))如果不设定结束点的话,那么就会直接数到最后。

如何运用PYTHON里字符串的count方法

6、print(abc.count("i",0,15))print(abc.count("a",0,15))print(abc.count("e",0,15))如果起始点为0的话,就是从开始数起来。

如何运用PYTHON里字符串的count方法

7、print(abc.count(&孥恶膈茯quot;i",,15))print(abc.count("a"荑樊综鲶,,15))print(abc.count("e",,15))但是如果有结束点,没有起始点的话就会出现错误,不能为空集。

如何运用PYTHON里字符串的count方法

© 一点知识