📌  相关文章
📜  Python - 测试字符串是否包含任何大写字符

📅  最后修改于: 2022-05-13 01:55:35.655000             🧑  作者: Mango

Python - 测试字符串是否包含任何大写字符

给定一个字符串,测试它是否包含任何大写字符。

方法 #1:使用循环 + isupper()

在此,我们对 String 中的每个字符进行迭代,使用isupper()检查大写,如果找到,则将 String 标记为 True。

Python3
# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using isupper() + loop
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
res = False
for ele in test_str:
  
    # checking for uppercase character and flagging
    if ele.isupper():
        res = True
        break
  
# printing result
print("Does String contain uppercase character : " + str(res))


Python3
# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using any() + isupper()
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using any() to check for any element to be uppercase
res = any(ele.isupper() for ele in test_str)
  
# printing result
print("Does String contain uppercase character : " + str(res))


Python3
# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using re()
import re
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using regex to check for any element to be uppercase
res = bool(re.match(r'\w*[A-Z]\w*', test_str))
  
# printing result
print("Does String contain uppercase character : " + str(res))


Python3
# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using any() + ASCII values
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using ascii values check for any element to be uppercase
res = any(ord(ele) != 32 and ord(ele) <=
          64 or ord(ele) >= 91 for ele in test_str)
  
# printing result
print("Does String contain uppercase character : " + str(res))


输出:

The original string is : geeksforGeeks
Does String contain uppercase character : True

方法#2:使用 any() + isupper()

在这里,我们使用any()来检查任何字符是否为大写字符。

蟒蛇3

# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using any() + isupper()
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using any() to check for any element to be uppercase
res = any(ele.isupper() for ele in test_str)
  
# printing result
print("Does String contain uppercase character : " + str(res))

输出:

The original string is : geeksforGeeks
Does String contain uppercase character : True

方法#3:使用regex()

可以使用适当的正则表达式来执行此任务。这会检查字符串中的任何大写字母。

蟒蛇3

# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using re()
import re
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using regex to check for any element to be uppercase
res = bool(re.match(r'\w*[A-Z]\w*', test_str))
  
# printing result
print("Does String contain uppercase character : " + str(res))

输出:

The original string is : geeksforGeeks
Does String contain uppercase character : True

方法 #4:使用 any() + ASCII 值

检查每个字符是否在 ASCII 值的大写池中。

蟒蛇3

# Python3 code to demonstrate working of
# Test if String contains any Uppercase character
# Using any() + ASCII values
  
# initializing string
test_str = 'geeksforGeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# Using ascii values check for any element to be uppercase
res = any(ord(ele) != 32 and ord(ele) <=
          64 or ord(ele) >= 91 for ele in test_str)
  
# printing result
print("Does String contain uppercase character : " + str(res))

输出:

The original string is : geeksforGeeks
Does String contain uppercase character : True