利用python找出字符串“nice to meet you!”中字符‘t'的个数和位置;并写出代码
时间: 2023-05-11 09:02:58 浏览: 286
代码如下:
```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]
```
相关问题
找出字符串 nice to meet you 中字符 t 的个数和 位置 代码
字符串 "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]
```