如何遍历输入的字符串,并判断字符是否为大写字母、小写字母、数字或其他符号
时间: 2024-01-30 21:03:04 浏览: 35
你可以使用循环遍历字符串中的每个字符,使用Python内置函数isupper,islower和isdigit来判断字符是否为大写字母、小写字母、数字。如果都不是,则可以判断为其他符号。例子代码如下:
```
string = "Hello, World! 123"
for char in string:
if char.isupper():
print(char, "is uppercase letter")
elif char.islower():
print(char, "is lowercase letter")
elif char.isdigit():
print(char, "is a number")
else:
print(char, "is a symbol")
```
输出结果为:
```
H is uppercase letter
e is lowercase letter
l is lowercase letter
l is lowercase letter
o is lowercase letter
, is a symbol
is a symbol
W is uppercase letter
o is lowercase letter
r is lowercase letter
l is lowercase letter
d is lowercase letter
! is a symbol
is a symbol
1 is a number
2 is a number
3 is a number
```
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)