Kiểm tra xem danh sách có chứa chuỗi rỗng Python không

Làm cách nào để kiểm tra xem Chuỗi Python có trống hay không? . Các chuỗi rỗng được coi là false có nghĩa là chúng sai trong ngữ cảnh Boolean. Kiểm tra trống trên một chuỗi là một biểu thức rất phổ biến và được sử dụng nhiều nhất trong bất kỳ ngôn ngữ lập trình nào, kể cả python

Các phương pháp để kiểm tra xem một chuỗi có trống hay không

  1. Vì chuỗi rỗng là False nên toán tử not được sử dụng để kiểm tra xem chuỗi có rỗng hay không
  2. Lưu ý rằng một chuỗi chỉ có khoảng trắng không được coi là trống, do đó, bạn cần loại bỏ khoảng trắng trước khi kiểm tra xem có trống không
  3. Hàm
    
    # Consider the empty string without spaces
    first = ""
    if(len(first) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the empty string with spaces
    second = " "
    if(len(second) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the string 
    third = "sparkby"
    if(len(third) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Output:
    # Empty string
    # Not empty string
    # Not empty string
    
    0 cũng có thể được sử dụng để kiểm tra xem chuỗi có rỗng hay không
  4. Toán tử
    
    # Consider the empty string without spaces
    first = ""
    if(len(first) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the empty string with spaces
    second = " "
    if(len(second) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the string 
    third = "sparkby"
    if(len(third) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Output:
    # Empty string
    # Not empty string
    # Not empty string
    
    1 là một cách khác được sử dụng nhiều nhất để kiểm tra chỗ trống
  5. 
    # Consider the empty string without spaces
    first = ""
    if(len(first) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the empty string with spaces
    second = " "
    if(len(second) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the string 
    third = "sparkby"
    if(len(third) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Output:
    # Empty string
    # Not empty string
    # Not empty string
    
    2 là một cách thanh lịch để kiểm tra
  6. Hàm
    
    # Consider the empty string without spaces
    first = ""
    if(len(first) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the empty string with spaces
    second = " "
    if(len(second) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the string 
    third = "sparkby"
    if(len(third) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Output:
    # Empty string
    # Not empty string
    # Not empty string
    
    3 được sử dụng để lấy độ dài của chuỗi và kiểm tra xem nó có phải là
    
    # Consider the empty string without spaces
    first = ""
    if(len(first) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the empty string with spaces
    second = " "
    if(len(second) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Consider the string 
    third = "sparkby"
    if(len(third) == 0):
      print("Empty string")
    else:
      print("Not empty string")
    
    # Output:
    # Empty string
    # Not empty string
    # Not empty string
    
    4 hay không để xem chuỗi có trống không
Chuỗi rỗng Kiểm tra biểu thứcResult______0_______5True khi một chuỗi rỗng
Sai khi một chuỗi có giá trị.

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
6Sai khi chuỗi rỗng
Đúng khi một chuỗi có giá trị.

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
7Xóa các khoảng trống và kiểm tra xem có trống không.

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
8Sai khi một chuỗi có giá trị
Đúng khi một chuỗi rỗng.

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
9True khi một chuỗi rỗng
Sai khi một chuỗi có giá trị.

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
0True khi một chuỗi rỗng
Sai khi một chuỗi có giá trị.

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
1True khi một chuỗi rỗng
Sai khi một chuỗi có giá trị. Bảng biểu thức kiểm tra chuỗi rỗng trong Python

1. Ví dụ nhanh về kiểm tra xem chuỗi có trống không

Sau đây là các ví dụ nhanh về cách kiểm tra xem chuỗi đã cho có trống hay không trong python


# Quick Examples 

# Using not to check if string is empty
print(not "")          # => True
print(not " ")         # => False
print(not " ".strip()) # => True
print(not "test")      # => False
print(not None)        # => True

# Using bool()
print(bool(""))          # => False
print(bool(" "))         # => True
print(bool(" ".strip())) # => False
print(bool("test"))      # => True
print(bool(None))        # => False

# Using == operator
print("" == "")          # => True
print(" " == "")         # => False
print(" ".strip() == "") # => True
print("test" == "")      # => False
print(None == "")        # => False

# Using __eq__()
print("".__eq__(""))         # => True
print("".__eq__(" "))        # => False
print("".__eq__(" ".strip()))# => True
print("".__eq__("test"))     # => False
print("".__eq__(None))       # => NotImplemented

# Using len()
print(len("") == 0)          # => True
print(len(" ") == 0)         # => False
print(len(" ".strip()) == 0) # => True
print(len("test") == 0)      # => False
print(len(None) == 0)        # => Error

2. Kiểm tra Chuỗi trống bằng cách sử dụng len()

Python


# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
3 được sử dụng để lấy tổng số ký tự có trong chuỗi và kiểm tra xem độ dài của chuỗi có bằng 0 hay không để xác định chuỗi có trống không. Trên thực tế, hàm len() trả về độ dài của một đối tượng. Đối tượng có thể là một chuỗi, một danh sách, một bộ hoặc các đối tượng khác hỗ trợ phương thức

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
3

Hãy tạo ba chuỗi. Chuỗi đầu tiên trống, chuỗi thứ hai trống nhưng bao gồm khoảng trắng và chuỗi thứ ba là chuỗi rỗng. Tôi sẽ sử dụng chúng trong ví dụ dưới đây để kiểm tra xem chuỗi có rỗng hay không


# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string

3. Chuỗi kiểm tra trống không sử dụng toán tử

Toán tử not là toán tử logic trả về


# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
5 nếu giá trị hoặc biểu thức là FalseFalse nếu giá trị hoặc biểu thức là

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
5. Vì vậy, nếu chúng ta chỉ định not trước chuỗi trong điều kiện if, Nó sẽ trở thành True khi chuỗi trống và Sai khi chuỗi không trống


# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string

Giải trình

  1. chuỗi đầu tiên trống, vì vậy nó sẽ trở thành Sai bên trong nếu điều kiện. không sẽ chuyển Sai thành Đúng. Do đó câu lệnh bên trong điều kiện if được thực thi
  2. chuỗi thứ hai không trống, vì vậy nó sẽ trở thành True bên trong nếu điều kiện. không sẽ chuyển đổi Đúng thành Sai. Do đó, câu lệnh bên trong khối khác được thực thi
  3. chuỗi cuối cùng không trống, vì vậy nó sẽ trở thành True bên trong nếu điều kiện. không sẽ chuyển đổi Đúng thành Sai. Do đó, câu lệnh bên trong khối khác được thực thi

5. Kiểm tra xem Chuỗi có trống không bằng cách sử dụng bool()

Hàm


# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
0 là một hàm tích hợp trả về giá trị Boolean của một đối tượng được chỉ định. Nó trả về

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
5 nếu chuỗi không trống và False nếu chuỗi trống. Vì chuỗi rỗng được coi là "falsy" và sẽ đánh giá thành False khi được chuyển đến

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
0. Tất cả các đối tượng khác được coi là "trung thực" và sẽ đánh giá là

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
5


# Consider the empty string without spaces
first=""
if(bool(first)):
  print("Not empty string")
else:
  print("Empty string")

# Consider the empty string with spaces
second="  "
if(bool(second)):
  print("Not empty string")
else:
  print("Empty string")

# Consider the string 
third="sparkby"
if(bool(third)):
  print("Not empty string")
else:
  print("Empty string")

# Output:
# Empty string
# Not empty string
# Not empty string

Giải trình

  1. chuỗi đầu tiên trống, vì vậy bool() sẽ trả về Sai bên trong nếu điều kiện. Do đó, câu lệnh bên trong khối khác được thực thi
  2. chuỗi thứ hai không trống, vì vậy bool() sẽ trả về True bên trong nếu điều kiện. Do đó câu lệnh bên trong điều kiện if được thực thi
  3. chuỗi cuối cùng không trống, vì vậy bool() sẽ trả về True bên trong nếu điều kiện. Do đó câu lệnh bên trong điều kiện if được thực thi

6. Sử dụng == Toán tử

Toán tử


# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
1 được sử dụng để kiểm tra sự bằng nhau giữa hai giá trị. Nó trả về

# Consider the empty string without spaces
first=""
if(not first):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second="  "
if(not second):
  print("Empty string")
else:
  print("Not empty string")
# Consider the string 
third="sparkby"

if(not third):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
5 nếu các giá trị bằng nhau và False nếu chúng không bằng nhau. Bạn có thể sử dụng toán tử

# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
1 để so sánh các giá trị thuộc bất kỳ loại nào, bao gồm số nguyên, số float, chuỗi và đối tượng. Trong ví dụ dưới đây, tôi đang sử dụng nó với một biến chuỗi và so sánh nó với chuỗi rỗng

# Consider the empty string without spaces
first=""
if(bool(first)):
  print("Not empty string")
else:
  print("Empty string")

# Consider the empty string with spaces
second="  "
if(bool(second)):
  print("Not empty string")
else:
  print("Empty string")

# Consider the string 
third="sparkby"
if(bool(third)):
  print("Not empty string")
else:
  print("Empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
9, tương tự, bạn cũng có thể sử dụng nó với các ký tự chuỗi để kiểm tra sự bằng nhau của chuỗi


first=""
if "" == first:
   print("Empty string")
else:
   print("Not empty string")

# Output:
# Empty string

7. Sử dụng __eq__()

Khi bạn sử dụng toán tử == để kiểm tra xem một chuỗi có trống hay không, bên trong nó sẽ sử dụng hàm __eq__() của python. do đó, nếu bạn muốn, bạn có thể trực tiếp sử dụng chức năng này

Phương thức


# Consider the empty string without spaces
first = ""
if(len(first) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the empty string with spaces
second = " "
if(len(second) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Consider the string 
third = "sparkby"
if(len(third) == 0):
  print("Empty string")
else:
  print("Not empty string")

# Output:
# Empty string
# Not empty string
# Not empty string
2 là một phương thức đặc biệt, nó là một trong những phương thức được gọi là “dunder” (viết tắt của “double underscore”), là những phương thức đặc biệt trong Python có một cú pháp cụ thể, bắt đầu và kết thúc bằng hai dấu gạch dưới


# Using __eq__()
first=""
if ""__.eq__(first):
   print("Empty string")
else:
   print("Not empty string")

# Output:
# Empty string

8. Sử dụng khả năng hiểu danh sách để kiểm tra chuỗi có trống không

Cuối cùng, hãy sử dụng khả năng hiểu danh sách python để kiểm tra xem chuỗi có rỗng hay không


# Using list comprehension
first=""
x=["Not empty string" if len(first)>0 else "Empty string"]
print(x)

# Output:
# Empty string

6. Phần kết luận

Trong bài viết này, bạn đã học được các cách khác nhau để kiểm tra xem chuỗi có rỗng hay không trong Python. Vì các chuỗi rỗng được coi là sai trong Python, nên chúng ta có thể trực tiếp sử dụng nó với điều kiện if để kiểm tra hoặc sử dụng nó với toán tử not hoặc hàm bool(). Và cũng đã học cách sử dụng toán tử == và __eq__() để kiểm tra xem chuỗi có rỗng hay không