Hướng dẫn python check if string contains underscore - python kiểm tra xem chuỗi có chứa dấu gạch dưới không

Tôi phải viết một chương trình kiểm tra xem tên biến của người dùng có hợp lệ không. Đây là những quy tắc tôi đang kiểm tra: chỉ cho phép các chữ cái, số và ký tự dấu gạch dưới. Nhân vật đầu tiên không thể là một số.

Tôi không chắc mình đang làm gì sai. Mỗi lần tôi chạy chương trình, nó cho tôi biết rằng đó là một biến hợp lệ, ngay cả đối với các đầu vào không hợp lệ.

Đây là chương trình của tôi:

import string

def valid(name):
    for character in (name):
        if name[0].isdigit():
            name==False
            break 
        else:
            if character.isalnum() and character is ('_'):
                name==True
            else:
                name==False
    return

def main():
    name=input("Enter your Python variable name: ")

    while name != "done":
        valid(name)

        if True:
            print("This is a valid variable name.")

        else:
            print("This is not a valid variable name.")

        name=input("Enter your Python variable name or 'done': ")

main()

Dưới đây là đầu ra mẫu (nếu được thực hiện đúng):

Enter your Python variable name: high_temp
This is a valid variable name.

Enter your Python variable name or "done": highTemp2
This is a valid variable name.

Enter your Python variable name or "done": 2_high_temp
This is not a valid variable name.

Enter your Python variable name or "done": done

Cảm ơn bạn trước!

Đã hỏi ngày 8 tháng 11 năm 2015 lúc 18:54Nov 8, 2015 at 18:54

Hướng dẫn python check if string contains underscore - python kiểm tra xem chuỗi có chứa dấu gạch dưới không

2

Tôi sẽ viết nó theo cách này:

def valid(name):
    return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)

Nhưng có lẽ điều này sẽ có ý nghĩa hơn bây giờ:

def valid(name):
    if name[0].isdigit():
       return False
    for c in name:
       if not (c.isalnum() or c == '_'):
          return False
    return True

def main():

    while True:

        name = input("Enter your Python variable name or 'done': ")

        if name == "done":
           break

        if valid(name):
            print("This is a valid variable name.")
        else:
            print("This is not a valid variable name.")

main()

Đã trả lời ngày 8 tháng 11 năm 2015 lúc 19:21Nov 8, 2015 at 19:21

Dan D.Dan D.Dan D.

71.6K13 Huy hiệu vàng98 Huy hiệu bạc119 Huy hiệu đồng13 gold badges98 silver badges119 bronze badges

0

Có vẻ như bạn có thể thoát khỏi phương pháp

Enter your Python variable name: high_temp
This is a valid variable name.

Enter your Python variable name or "done": highTemp2
This is a valid variable name.

Enter your Python variable name or "done": 2_high_temp
This is not a valid variable name.

Enter your Python variable name or "done": done
2

return name.isidentifier()

Hướng dẫn python check if string contains underscore - python kiểm tra xem chuỗi có chứa dấu gạch dưới không

Đã trả lời ngày 9 tháng 7 năm 2017 lúc 1:43Jul 9, 2017 at 1:43

1

Một giải pháp pythonic khác,

import string

def valid(name):
   if name[0].isdigit():
       return false
   accepted_chars=string.digits + string.ascii_lowercase + '_'
   return all(item for name if item.lower() in accepted_chars)

Đã trả lời ngày 8 tháng 11 năm 2015 lúc 19:34Nov 8, 2015 at 19:34

Hướng dẫn python check if string contains underscore - python kiểm tra xem chuỗi có chứa dấu gạch dưới không

Adem Öztaşadem ÖztaşAdem Öztaş

19.4K4 Huy hiệu vàng33 Huy hiệu bạc42 Huy hiệu đồng4 gold badges33 silver badges42 bronze badges

if True

luôn luôn là

Enter your Python variable name: high_temp
This is a valid variable name.

Enter your Python variable name or "done": highTemp2
This is a valid variable name.

Enter your Python variable name or "done": 2_high_temp
This is not a valid variable name.

Enter your Python variable name or "done": done
3. Bạn có ý nói

if valid(name)

Bạn chỉ cần cuộc gọi này đến

Enter your Python variable name: high_temp
This is a valid variable name.

Enter your Python variable name or "done": highTemp2
This is a valid variable name.

Enter your Python variable name or "done": 2_high_temp
This is not a valid variable name.

Enter your Python variable name or "done": done
4

Đã trả lời ngày 8 tháng 11 năm 2015 lúc 18:58Nov 8, 2015 at 18:58

Saulspatzsaulspatzsaulspatz

4.8215 Huy hiệu vàng33 Huy hiệu bạc42 Huy hiệu đồng5 gold badges33 silver badges42 bronze badges

1

Làm cách nào để kiểm tra xem một chuỗi có chứa một dấu chấm câu không?

Để kiểm tra xem một chuỗi có ký tự đặc biệt hay không trong Python. Chúng ta có thể sử dụng các hàm re.match (), re.Search (), re.sub () để phát hiện các ký tự đặc biệt từ chuỗi.

Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một ký tự đặc biệt trong Python không?

  • Cách tiếp cận: Tạo đối tượng biểu thức thông thường (Regex) của tất cả các ký tự đặc biệt mà chúng ta không muốn, sau đó chuyển một chuỗi trong phương thức tìm kiếm. Nếu bất kỳ một ký tự nào của chuỗi phù hợp với đối tượng Regex thì phương thức tìm kiếm sẽ trả về một đối tượng khớp khác không trả về không.
  • Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một chữ cái hoặc số trong Python?
  • Làm cách nào để kiểm tra xem một chuỗi có chứa một dấu chấm câu không?

    Để kiểm tra xem một chuỗi có ký tự đặc biệt hay không trong Python. Chúng ta có thể sử dụng các hàm re.match (), re.Search (), re.sub () để phát hiện các ký tự đặc biệt từ chuỗi.

    Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một ký tự đặc biệt trong Python không?

    Cách tiếp cận: Tạo đối tượng biểu thức thông thường (Regex) của tất cả các ký tự đặc biệt mà chúng ta không muốn, sau đó chuyển một chuỗi trong phương thức tìm kiếm. Nếu bất kỳ một ký tự nào của chuỗi phù hợp với đối tượng Regex thì phương thức tìm kiếm sẽ trả về một đối tượng khớp khác không trả về không. Regular Expression in Python

    Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một chữ cái hoặc số trong Python?For example, the user has a string for the username of a person and the user doesn’t want the username to have any special characters such as @, $, etc. 

    Isalnum () là một hàm Python tích hợp để kiểm tra xem tất cả các ký tự trong một chuỗi là chữ và số. Nói cách khác, isalnum () kiểm tra xem một chuỗi chỉ chứa các chữ cái hoặc số hoặc cả hai. Nếu tất cả các ký tự là chữ và số, isalnum () trả về giá trị đúng; Nếu không, phương thức trả về giá trị sai.

    Phương pháp 1: Sử dụng biểu thức chính quy. Using Regular Expression.

    Có một hàm trong Thư viện biểu thức thông thường (RE) so sánh hai chuỗi cho chúng tôi. Re.Match (Mẫu, Chuỗi) là một hàm trả về một đối tượng, để tìm liệu một trận đấu có tìm thấy hay không chúng ta phải đánh máy nó vào boolean. & nbsp;re) that compares two string for us. re.match(pattern, string) is a function that returns an object, to find whether a match is find or not we have to typecast it into boolean. 

    Cú pháp: re.match (mẫu, chuỗi) & nbsp; re.match(pattern, string) 

    Parameters:

    • Mẫu: mẫu mà bạn muốn kiểm tra & nbsp; the pattern against which you want to check 
    • Chuỗi: Chuỗi bạn muốn kiểm tra mẫu & nbsp; the string you want to check for the pattern 

    Trả về: Đối tượng khớp & nbsp; Match object 

    Hãy để xem một ví dụ:

    Ví dụ 1:

    Python3

    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    5
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    6

    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    7
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    9

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    0
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    2

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    3
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    5
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    6

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    8

    Output:

    True

    Ví dụ 2:

    Python3

    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    5
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    6

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    5
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    4
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    9
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    6

    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    8
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    9

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    5
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    4
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    9
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    6

    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    7
    return name.isidentifier()
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    9

    Output:

    True
    False

    Phương pháp 2: Sử dụng Set. Using Set.

    Bộ được tích hợp loại dữ liệu trong Python. Chúng tôi đang sử dụng hàm set của bộ trả về đúng nếu tất cả các ký tự của một bộ có mặt trong một tập hợp nhất định khác. issubset()function of set that returns True if all characters of a set are present in a given set Otherwise False.

    Cú pháp: set_name.issubset (set) tham số: & nbsp;set_name.issubset(set)
    Parameters: 

    • Đặt: Đại diện cho bộ mà tập hợp con phải được tìm kiếm & nbsp; Represents that set in which the subset has to be searched 

    Trả lại: Giá trị Boolean boolean value

    Hãy cùng xem một ví dụ: Ví dụ 1: & nbsp;
    Example 1: 

    Python3

    return name.isidentifier()
    
    9
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    1
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    22____53
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    4

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    0
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    7

    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    8
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    1
    if True
    
    1

    if True
    
    2
    if True
    
    3

    if True
    
    4
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    if True
    
    7
    if True
    
    8

    if True
    
    9
    if valid(name)
    
    0

    if True
    
    4
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    if valid(name)
    
    4
    if True
    
    8

    Output:

    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    0

    Ví dụ 2: & nbsp; 

    Python3

    return name.isidentifier()
    
    9
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    1
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    3
    if True
    
    8

    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    0
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    True
    4

    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    8
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    1
    if True
    
    1

    if True
    
    2
    if True
    
    3

    if True
    
    4
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    if True
    
    7
    if True
    
    8

    if True
    
    9
    if valid(name)
    
    0

    if True
    
    4
    def valid(name):
        return not name[0].isdigit() and all(c.isalnum() or c == '_' for c in name)
    
    7
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    if valid(name)
    
    4
    if True
    
    8

    Output:

    True

    Ví dụ 2: & nbsp;

    Python3O(n)

    return name.isidentifier()
    
    9
    Enter your Python variable name: high_temp
    This is a valid variable name.
    
    Enter your Python variable name or "done": highTemp2
    This is a valid variable name.
    
    Enter your Python variable name or "done": 2_high_temp
    This is not a valid variable name.
    
    Enter your Python variable name or "done": done
    
    8
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    1
    def valid(name):
        if name[0].isdigit():
           return False
        for c in name:
           if not (c.isalnum() or c == '_'):
              return False
        return True
    
    def main():
    
        while True:
    
            name = input("Enter your Python variable name or 'done': ")
    
            if name == "done":
               break
    
            if valid(name):
                print("This is a valid variable name.")
            else:
                print("This is not a valid variable name.")
    
    main()
    
    2
    import string
    
    def valid(name):
       if name[0].isdigit():
           return false
       accepted_chars=string.digits + string.ascii_lowercase + '_'
       return all(item for name if item.lower() in accepted_chars)
    
    3
    if True
    
    8
    O(n)


    Làm cách nào để kiểm tra xem một chuỗi có chứa một dấu chấm câu không?

    Để kiểm tra xem một chuỗi có ký tự đặc biệt hay không trong Python. Chúng ta có thể sử dụng các hàm re.match (), re.Search (), re.sub () để phát hiện các ký tự đặc biệt từ chuỗi.re. match(), re.search(), re. sub() functions for detecting the special characters from the string.

    Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một ký tự đặc biệt trong Python không?

    Cách tiếp cận: Tạo đối tượng biểu thức thông thường (Regex) của tất cả các ký tự đặc biệt mà chúng ta không muốn, sau đó chuyển một chuỗi trong phương thức tìm kiếm.Nếu bất kỳ một ký tự nào của chuỗi phù hợp với đối tượng Regex thì phương thức tìm kiếm sẽ trả về một đối tượng khớp khác không trả về không.Make a regular expression(regex) object of all the special characters that we don't want, then pass a string in search method. If any one character of string is matching with regex object then search method returns a match object otherwise return None.

    Làm thế nào để bạn kiểm tra xem một chuỗi có chứa một chữ cái hoặc số trong Python?

    Isalnum () là một hàm Python tích hợp để kiểm tra xem tất cả các ký tự trong một chuỗi là chữ và số.Nói cách khác, isalnum () kiểm tra xem một chuỗi chỉ chứa các chữ cái hoặc số hoặc cả hai.Nếu tất cả các ký tự là chữ và số, isalnum () trả về giá trị đúng;Nếu không, phương thức trả về giá trị sai. is a built-in Python function that checks whether all characters in a string are alphanumeric. In other words, isalnum() checks whether a string contains only letters or numbers or both. If all characters are alphanumeric, isalnum() returns the value True ; otherwise, the method returns the value False .

    Làm cách nào để xác minh rằng một chuỗi chỉ chứa các số chữ cái nhấn mạnh và dấu gạch ngang trong Java?

    Bạn có thể sử dụng các biểu thức thường xuyên để đạt được nhiệm vụ này.Để xác minh rằng chuỗi chỉ chứa các chữ cái, số, dấu gạch dưới và dấu gạch ngang, chúng ta có thể sử dụng regex sau: "^[a-za-z0-9 _-]*$"."^[A-Za-z0-9_-]*$".