python计算字符串中的大写
时间: 2024-04-14 16:29:48 浏览: 73
可以使用Python中的isupper()方法来判断一个字符是否为大写字母,然后遍历字符串中的每个字符,统计大写字母的个数即可。以下是示例代码:
```python
s = "Hello World"
count =
for c in s:
if c.isupper():
count += 1
print("大写字母个数为:", count)
```
输出结果为:
```
大写字母个数为: 2
```
相关问题
python计算字符串中大写字母个数
可以使用Python中的isupper()方法来判断一个字符是否为大写字母,然后遍历字符串中的每个字符,统计大写字母的个数即可。以下是示例代码:
```python
s = "Hello World"
count =
for c in s:
if c.isupper():
count += 1
print("大写字母个数为:", count)
```
输出结果为:
```
大写字母个数为: 2
```
编写程序,要求输入字符串,计算字符串中大写字母和小写字母的个数并输出,计算过程用函数实现
```python
def count_upper_lower(s):
count_upper = 0
count_lower = 0
for c in s:
if c.isupper():
count_upper += 1
elif c.islower():
count_lower += 1
return count_upper, count_lower
s = input("请输入字符串:")
count_upper, count_lower = count_upper_lower(s)
print("大写字母个数:", count_upper)
print("小写字母个数:", count_lower)
```
阅读全文