Python输入一个字符串,统计它包含的大写字母的个数
时间: 2024-02-16 21:34:40 浏览: 13
可以使用isupper()函数判断一个字符是否为大写字母,然后遍历字符串中的每个字符,统计大写字母的个数。
代码如下:
```python
string = input("请输入一个字符串:")
count = 0
for char in string:
if char.isupper():
count += 1
print("字符串中包含的大写字母个数为:", count)
```
运行结果示例:
```
请输入一个字符串:Hello World!
字符串中包含的大写字母个数为: 2
```
相关问题
python统计一个字符串中大写字母的个数
可以使用Python内置函数isupper()来判断一个字符是否为大写字母,然后再遍历整个字符串,统计大写字母的个数。
示例代码如下:
```python
s = 'Hello World!'
count = 0
for c in s:
if c.isupper():
count += 1
print(count)
```
输出结果为:
```
2
```
其中,字符串s中包含2个大写字母'H'和'W'。
用python统计一个字符串中大写字母的个数
非常感谢您的提问。您可以使用以下代码来统计一个字符串中大写字母的个数:
```
string = "Hello World"
count = sum(1 for c in string if c.isupper())
print(count)
```
这段代码可以输出字符串中大写字母的个数,即2。希望能对您有所帮助。