统计一个大字符串中,小字符串出现的次数
时间: 2023-11-15 20:06:05 浏览: 27
可以使用Python中的count方法来统计一个大字符串中小字符串出现的次数,示例代码如下:
```python
big_str = "Hello world, welcome to the world of Python programming!"
small_str = "world"
count = big_str.count(small_str)
print(f"The small string '{small_str}' appears {count} times in the big string.")
```
输出结果为:
```
The small string 'world' appears 2 times in the big string.
```
如果需要忽略大小写,可以先将大字符串和小字符串都转换为小写或大写形式再进行统计,示例代码如下:
```python
big_str = "Hello world, welcome to the world of Python programming!"
small_str = "WORLD"
big_str_lower = big_str.lower()
small_str_lower = small_str.lower()
count = big_str_lower.count(small_str_lower)
print(f"The small string '{small_str}' appears {count} times (case-insensitive) in the big string.")
```
输出结果为:
```
The small string 'WORLD' appears 2 times (case-insensitive) in the big string.
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)