📌  相关文章
📜  Python程序检查字符串中是否存在小写字母

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

Python程序检查字符串中是否存在小写字母

给定一个字符串,任务是编写一个Python程序来检查字符串是否有小写字母。

例子:

Input: "Live life to the fullest"
Output: false

Input: "LIVE LIFE TO THe FULLEST"
Output: false

Input: "LIVE LIFE TO THE FULLEST"
Output: true

方法 1#:使用 islower()

如果字符串字符小写并且至少有一个大小写字符,则返回 true ,否则返回 false 。

Python
# Python code to demonstrate working of
# is.lower()  method in strings
  
#Take any string
str = "Live life to the Fullest"
  
# Traversing Each character of
# the string to check whether
# it is in lowercase
for char in str:
    k = char.islower()  
    if k == True:
        print('True')
          
    # Break the Loop when you
    # get any lowercase character.
        break  
  
# Default condition if the string
# does not have any lowercase character.
if(k != 1):
    print('False')


Python
# Python Program to demonstrate the
# is.upper() method in strings
#Take a string
str = "GEEKS"
  
# Traversing Each character
# to check whether it is in uppercase
for char in str:
    k = char.isupper()  
    if k == False:
        print('False')
          
        # Break the Loop
        # when you get any
        # uppercase character.
        break
  
# Default condition if the string
# does not have any uppercase character.
if(k == 1):
    print('True')


Python3
# Python Program to Demonstrate the strings
# methods to check whether given Character
# is in uppercase or lowercase with the help
# of ASCII values
  
#Input by geeks
x = "GEeK"
  
# counter
counter = 0
  
for char in x:
    if(ord(char) >= 65 and ord(char) <= 90):
        
        counter = counter + 1
          
    # Check for the condition 
    # if the ASCII value is Between 97 and 122
    # if condition is True print the
    # corresponding result
    if(ord(char) >= 97 and ord(char) <= 122):
        print("Lower Character found")
        break
if counter == len(x):
    print(True)


输出:

True

方法 2#:使用 isupper()

如果字符串字符大写并且至少有一个大小写字符,则返回 true ,否则返回 false 。

Python

# Python Program to demonstrate the
# is.upper() method in strings
#Take a string
str = "GEEKS"
  
# Traversing Each character
# to check whether it is in uppercase
for char in str:
    k = char.isupper()  
    if k == False:
        print('False')
          
        # Break the Loop
        # when you get any
        # uppercase character.
        break
  
# Default condition if the string
# does not have any uppercase character.
if(k == 1):
    print('True')

输出:

True

方法 3#:使用 ASCII 值检查给定字符是大写还是小写。

ord()函数返回一个表示 Unicode字符的整数。

例子:

print(ord('A'))    # 65
print(ord('a'))    # 97

蟒蛇3

# Python Program to Demonstrate the strings
# methods to check whether given Character
# is in uppercase or lowercase with the help
# of ASCII values
  
#Input by geeks
x = "GEeK"
  
# counter
counter = 0
  
for char in x:
    if(ord(char) >= 65 and ord(char) <= 90):
        
        counter = counter + 1
          
    # Check for the condition 
    # if the ASCII value is Between 97 and 122
    # if condition is True print the
    # corresponding result
    if(ord(char) >= 97 and ord(char) <= 122):
        print("Lower Character found")
        break
if counter == len(x):
    print(True)

输出:

Lower Character found