Python nếu chuỗi không phải là

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

________số 8_______

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

Bạn có thể sử dụng. = Đối với chuỗi trong Python?

Bạn có thể so sánh các chuỗi trong Python bằng cách sử dụng đẳng thức [ == ] và so sánh [ < , > ,. =

Cái gì không phải là chuỗi trong Python?

Không gây nhầm lẫn, nhưng chuỗi byte là một chuỗi trong Python 2 chứ không phải chuỗi trong Python 3.

làm gì. = Có nghĩa là gì trong Python?

Toán tử không bình đẳng trong Python . Nếu các giá trị được so sánh bằng nhau thì trả về giá trị true. Nếu các giá trị được so sánh không bằng nhau, thì giá trị sai được trả về. . =

Chủ Đề