python打印字符串中的大写字符

  • 方式一,直接采用函数 isupper() 判断
  • 方式二,判断是否在区间
  • 方式三,直接比较ASCLL码
cmd = 'aHHUJbnxsdbZZZhniZnDnjhioSA'
temp = cmd.upper()  # 把这个字符串全部变为大写字符

# 方式一,直接采用函数 isupper() 判断
temp2 = ''
for c in cmd:
    if c.isupper():
        temp2 += c

print(temp)
print(temp2)
print(ord('A')) # ordinal,序数

# 方式二,判断是否在区间
temp3 = ''
for c in cmd:
    if 'A' <= c <= 'Z': # 这里比较的其实就是ASCLL码
        temp3 += c
print(temp3)

# 方式三,直接比较ASCLL码
temp4 = ''
for c in cmd:
    if 65 <= ord(c) <= 90:  # ord是ordinal的缩写,序数。65可替换为ord('A'),90可替换为ord('Z')
        temp4 += c
print(temp4)
AHHUJBNXSDBZZZHNIZNDNJHIOSA
HHUJZZZZDSA
65
HHUJZZZZDSA
HHUJZZZZDSA

猜你喜欢

转载自blog.csdn.net/LWD19981223/article/details/133936360