找出字符串 nice to meet you 中字符 t 的个数和 位置 代码
时间: 2024-05-12 14:20:58 浏览: 16
字符串 "nice to meet you" 中字符 t 的个数为 2,位置分别为第 2 和第 9 个字符。
代码示例:
```python
s = "nice to meet you"
count = 0
positions = []
for i in range(len(s)):
if s[i] == "t":
count += 1
positions.append(i)
print("字符 t 的个数为:", count)
print("字符 t 的位置为:", positions)
```
输出结果:
```
字符 t 的个数为: 2
字符 t 的位置为: [2, 9]
```
相关问题
用代码找出字符串“Nice To Meet You!”中字符“t”的个数和位置
```python
s = "Nice To Meet You!"
count = s.count("t")
positions = [i for i in range(len(s)) if s[i] == "t"]
print("字符t的个数为:", count)
print("字符t的位置为:", positions)
```
输出结果:
```
字符t的个数为: 2
字符t的位置为: [5, 11]
```
利用python找出字符串“nice to meet you!”中字符‘t'的个数和位置;并写出代码
代码如下:
```python
s = "nice to meet you!"
count = 0
positions = []
for i in range(len(s)):
if s[i] == 't':
count += 1
positions.append(i)
print("字符't'的个数为:", count)
print("字符't'的位置为:", positions)
```
输出结果为:
```
字符't'的个数为: 2
字符't'的位置为: [2, 11]
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)