Hướng dẫn how do you tell if a character is a letter in python? - làm thế nào để bạn biết nếu một ký tự là một ký tự trong python?

Tôi đã tìm thấy một cách tốt để làm điều này với việc sử dụng một chức năng và mã cơ bản. Đây là một mã chấp nhận một chuỗi và đếm số lượng chữ in hoa, chữ cái viết thường và cả 'khác'. Khác được phân loại là không gian, dấu chấm câu hoặc thậm chí các nhân vật Nhật Bản và Trung Quốc.

Show
def check(count):

    lowercase = 0
    uppercase = 0
    other = 0

    low = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
    upper = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'



    for n in count:
        if n in low:
            lowercase += 1
        elif n in upper:
            uppercase += 1
        else:
            other += 1

    print("There are " + str(lowercase) + " lowercase letters.")
    print("There are " + str(uppercase) + " uppercase letters.")
    print("There are " + str(other) + " other elements to this sentence.")

Làm thế nào để bạn xác định các chữ cái và số trong Python?

Phương thức python isalpha () trả về true nếu một chuỗi chỉ chứa các chữ cái. Python isnumeric () trả về true nếu tất cả các ký tự trong một chuỗi là số.

Trong bài viết này, bạn sẽ học cách kiểm tra xem một ký tự trong chuỗi có phải là một chữ cái trong Python không. Ở đây, chữ cái biểu thị bảng chữ cái và loại trừ tất cả các ký tự số và đặc biệt khác.

Khi bạn làm việc với các chuỗi, bạn thường phải đối mặt với một tình huống mà bạn cần kiểm tra xem tất cả các ký tự trong chuỗi có phải là chữ cái hay không. Giả sử bạn đang viết một chương trình yêu cầu tên người dùng và sau đó lưu trữ chi tiết của anh ấy/cô ấy. Đầu tiên, bạn cần xác minh tên người dùng bằng cách kiểm tra tất cả các ký tự được nhập bởi người dùng. Đối với điều này, hãy kiểm tra xem tất cả các ký tự có phải là chữ cái hay không.

Nếu bạn muốn tìm hiểu thêm về các chuỗi và danh sách trong Python, hãy truy cập các hướng dẫn của Python.

#take a string as an input from user

input_str=str(input("Enter your name: "))

#iterate over the input string using for loop

for ch in input_str:
 
  #return true if the character is alphabet otherwise return False

  res=ch.isalpha()
  
  print(ch,res )
 

Output:

Enter your name: jo$n12
j True
o True
$ False
n True
1 False
2 False

Bạn có thể trực tiếp áp dụng chức năng Isalpha trên chuỗi để kiểm tra xem chuỗi chỉ chứa bảng chữ cái hay không. Ví dụ

str1='Ali'

str2='[email protected]'

str3= 'David22'

print(str1.isalpha())

print(str2.isalpha())

print(str3.isalpha())
 

Output:

True
False
False

Bạn cũng có thể kiểm tra bất kỳ ký tự cụ thể nào bằng chỉ mục của nó. Giả sử bạn muốn kiểm tra ký tự thứ tư trong nhóm Str Str2. Bạn có thể làm điều này bằng cách chạy lệnh str2 [3] .Alpha (). Nhân vật thứ tư trong Str2 là Hồi! do đó không phải là bảng chữ cái do đó, chương trình sẽ in Sai False trên cửa sổ đầu ra. Hàm isalpha () trả về sai nếu tìm thấy không gian trong một chuỗi. Do đó, phương pháp này đã giành được công việc khi bạn phải xác minh các ký tự trong một chuỗi dài bao gồm các không gian hoặc một câu.

Sử dụng isalpha () và isspace () để kiểm tra xem ký tự trong chuỗi có phải không

Trong trường hợp các chuỗi bao gồm không gian, chúng ta có thể sử dụng hàm isspace (). Nó trả về một đúng nếu không gian được phát hiện khác trả về sai. Bằng cách kết hợp hàm isalpha () và isspace (), chúng ta có thể kiểm tra xem tất cả các ký tự trong một chuỗi là bảng chữ cái và không gian hay không.

def check_string(input_str):

  if all(x.isalpha() or x.isspace() for x in input_str):

    return True

  else:

    return False

if __name__ == "__main__" :

  string = input("Enter a string: ")

  print(check_string(string))
 

Output:

Enter a string: Hello World
True

Trong bài viết này, bạn đã học được cách xác định xem các ký tự trong một chuỗi có phải là một chữ cái hay không trong lập trình Python. Bạn cũng đã học được cách đối phó với các chuỗi câu bao gồm không gian. Nếu bạn có bất kỳ câu hỏi nào liên quan đến bài viết này, xin vui lòng cho chúng tôi biết trong phần bình luận. Liên hệ chúng tôi.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc isalpha() method is used to check whether all characters in the String is an alphabet.

    Bàn luận

    Phương thức Python StringAsalpha () được sử dụng để kiểm tra xem tất cả các ký tự trong chuỗi là bảng chữ cái.  string.isalpha()

    Chuỗi python isalpha () Phương thức cú pháp:isalpha() does not take any parameters

    Returns:

    • Cú pháp: & nbsp; String.isalpha (): If all characters in the string are alphabet.
    • Tham số: isalpha () không lấy bất kỳ tham số nào: If the string contains 1 or more non-alphabets.

    Đúng: nếu tất cả các ký tự trong chuỗi là bảng chữ cái.

    1. Sai: Nếu chuỗi chứa 1 hoặc nhiều không phải alphabet.
    2. Lỗi và ngoại lệ:
    3. Nó không chứa đối số, do đó xảy ra lỗi nếu tham số được truyền

    Cả bảng chữ cái viết hoa và chữ thường trở lại

    Python3

    Không gian không được coi là bảng chữ cái, do đó nó trả về “sai”

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    5

    Output:

    True

    Chuỗi python isalpha () Phương thức Ví dụ:

    Python3

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    1
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    3

    Ví dụ 1: Làm việc của Isalpha ()

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    1
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    8

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    5

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    5

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    0

    Output:  

    True
    False
    False

    #take a string as an input from user input_str=str(input("Enter your name: ")) #iterate over the input string using for loop for ch in input_str: #return true if the character is alphabet otherwise return False res=ch.isalpha() print(ch,res ) 1#take a string as an input from user input_str=str(input("Enter your name: ")) #iterate over the input string using for loop for ch in input_str: #return true if the character is alphabet otherwise return False res=ch.isalpha() print(ch,res ) 2 Enter your name: jo$n12 j True o True $ False n True 1 False 2 False3

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    1
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    Enter your name: jo$n12
    j True
    o True
    $ False
    n True
    1 False
    2 False
    8

    Input : string = 'Ayush Saxena'
    Output : 11
             AyushSaxena
    
    Input : string = 'Ayush0212'
    Output : 5
             Ayush

    Algorithm:

    1. Khởi tạo một chuỗi mới và bộ đếm biến thành 0. & nbsp;
    2. Đi qua ký tự chuỗi đã cho bằng ký tự lên đến chiều dài của nó, kiểm tra xem ký tự là bảng chữ cái. & NBSP;
    3. Nếu đó là bảng chữ cái, hãy tăng bộ đếm 1 và thêm nó vào một chuỗi mới, khác đi qua ký tự tiếp theo. & NBSP;
    4. In giá trị của bộ đếm và chuỗi mới.

    Python3

    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    1
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    Enter your name: jo$n12
    j True
    o True
    $ False
    n True
    1 False
    2 False
    8

    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    4
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    6

    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    7
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    9

    True
    False
    False
    0
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    9

    True
    False
    False
    3
    True
    False
    False
    4
    True
    False
    False
    5
    True
    False
    False
    6

    True
    False
    False
    7
    True
    False
    False
    8
    True
    False
    False
    9__12

    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    4
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    4
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    6
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    8

    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    4
    Enter a string: Hello World
    True
    
    0
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    6
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    Enter a string: Hello World
    True
    
    3

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    Enter a string: Hello World
    True
    
    5

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    Enter a string: Hello World
    True
    
    7

    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    1
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    Enter your name: jo$n12
    j True
    o True
    $ False
    n True
    1 False
    2 False
    3

    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    4
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    6

    True
    False
    False
    3
    True
    False
    False
    4
    True
    False
    False
    5
    True
    False
    False
    6

    True
    False
    False
    7
    True
    False
    False
    8
    True
    False
    False
    9__12

    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    4
    str1='Ali'
    
    str2='[email protected]'
    
    str3= 'David22'
    
    print(str1.isalpha())
    
    print(str2.isalpha())
    
    print(str3.isalpha())
     
    
    4
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    6
    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    2
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    8

    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    4
    Input : string = 'Ayush Saxena'
    Output : 11
             AyushSaxena
    
    Input : string = 'Ayush0212'
    Output : 5
             Ayush
    1
    def check_string(input_str):
    
      if all(x.isalpha() or x.isspace() for x in input_str):
    
        return True
    
      else:
    
        return False
    
    if __name__ == "__main__" :
    
      string = input("Enter a string: ")
    
      print(check_string(string))
     
    
    6__12

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    Enter a string: Hello World
    True
    
    5

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    4
    Input : string = 'Ayush Saxena'
    Output : 11
             AyushSaxena
    
    Input : string = 'Ayush0212'
    Output : 5
             Ayush
    8

    Output: 

    #take a string as an input from user
    
    input_str=str(input("Enter your name: "))
    
    #iterate over the input string using for loop
    
    for ch in input_str:
     
      #return true if the character is alphabet otherwise return False
    
      res=ch.isalpha()
      
      print(ch,res )
     
    
    0

    Độ phức tạp về thời gian: O (n) O(n)

    Không gian phụ trợ: O (n)O(n)


    Làm thế nào để bạn kiểm tra xem một nhân vật là một lá thư trong Python?

    Sử dụng phương thức isalpha () để xác định xem ký tự có phải là python có chức năng isalpha () tích hợp, trả về đúng hay không nếu ký tự là một chữ cái khác trả về sai. Sử dụng cho vòng lặp, qua chuỗi và áp dụng hàm Isalpha trên tất cả các ký tự. to determine if character is a letter Python has a built-in Isalpha() function which returns true if the character is a letter otherwise returns false. Using for loop, traverse over the string and apply isalpha function on all the characters.

    Làm thế nào để bạn biết nếu một nhân vật là một lá thư?

    Bạn có thể sử dụng phương thức ký tự.isletter (char c) để kiểm tra xem ký tự có phải là chữ cái hợp lệ không.Phương thức này sẽ trả về một giá trị thực cho một ký tự chữ cái hợp lệ và sai nếu ký tự không phải là một chữ cái hợp lệ.Character. isLetter(char c) method to check if a character is a valid letter. This method will return a true value for a valid letter characters and false if the character is not a valid letter.

    Làm thế nào để bạn kiểm tra xem một chuỗi là một chữ cái?

    Để kiểm tra xem một chuỗi chỉ có các chữ cái Unicode trong Java, chúng tôi sử dụng các phương thức isDigit () và charat () với các câu lệnh ra quyết định.Phương thức Isletter (Int CodePoint) xác định xem ký tự cụ thể (Unicode CodePoint) có phải là một chữ cái hay không.Nó trả về một giá trị boolean, đúng hoặc sai.

    Làm thế nào để bạn xác định các chữ cái và số trong Python?

    Phương thức python isalpha () trả về true nếu một chuỗi chỉ chứa các chữ cái.Python isnumeric () trả về true nếu tất cả các ký tự trong một chuỗi là số.. Python isnumeric() returns true if all characters in a string are numbers.