1636110000
Pythonの文字列データ型は不変です。つまり、一度定義した文字列は変更できません。 文字列をスペースでは実際には空の文字列ですが、非ゼロの大きさを有しています。したがって、len() または not 演算子を使用して空の文字列をチェックすると、文字列内の文字としてスペースがカウントされるため、スペースのある文字列は空の文字列としてカウントされません。
Pythonで空の文字列をチェックするには、次のいずれかの方法を使用できます。
Pythonのnot演算子は、スペースだけの文字列が空でないことを確認します。これは実際にはTrueであってはなりません。
# app.py
strA = ""
# checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Check if the strA is empty : The string is empty
文字列は空です。文字列にスペースを入れていないことに注意してください。そのため、空として返されます。文字列にスペースを入れると、空としてカウントされず、not演算子 はFalseを返します 。
# app.py
strA = " "
# checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Check if the strA with space is empty : No it is not empty
Pythonで空の文字列をチェックするには、len()関数を使用します。0が返された場合は、文字列が空であることを意味します。そうでなければ、そうではありません。
したがって、文字列に何かがある場合、それは空でない文字列としてカウントされます。それ以外の場合は、空の文字列です。
strA = ""
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここで、条件が0を返すためにFalseに なる場合 、それ以外の条件はTrueになり、文字列は空になります。
文字列に空白が含まれている場合、空の文字列としてカウントされません。
strA = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is not empty
文字列内のスペースは文字としてカウントされます。そのため、空をチェックするとFalseが返されます。
Pythonで純粋な空の文字列をチェックするには、len() + string.strip()メソッドを使用します。string.strip()メソッドは、文字列から空白を除去します。したがって、スペースが含まれている場合、strip()関数は削除し、文字列が空であるかどうか、またはlen()関数を使用していないかどうかを確認します。
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここでは、文字列にいくつのスペースを追加しても、すべてのスペースが削除され、文字列の長さがチェックされます。0が返された場合は、文字列が空であることを意味します。そうでなければ、そうではありません。
str = " KRUNAL "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is not empty
この例では、文字列に特定の文字が含まれているため、文字列が空ではないことがはっきりとわかります。したがって、len()メソッドは文字列の長さを返し、if条件はTrueを返します。
string.isspace()文字列にスペースが含まれているか、いない場合は機能をチェック。文字列にスペースが含まれている場合は、Trueを返します。それ以外の場合は、Falseを返します。
私たちは、の組み合わせを使用 文字列 と ないstring.isspace() 文字列が空であるかどうかにかかわらず、スペースのかどうかを確認する方法を。
このメソッドは、文字列に多くのスペースが含まれている場合に計算のロード時間がかかるストリップ操作を実行する必要があるため、strip()メソッドよりも効率的です。
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(str and not str.isspace()):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここでは、isspace()関数の負の条件を and演算子でチェックしています。
条件の1つがFalseになった場合、「and演算子」により、条件がFalse を返し、それ 以外の 場合は条件が実行されます。
これは、Pythonで純粋な空の文字列をチェックするためのより良い方法です。
このチュートリアルは以上です。
リンク:https://appdividend.com/2020/11/17/how-to-check-if-string-is-empty-or-not-in-python/
1636110000
Pythonの文字列データ型は不変です。つまり、一度定義した文字列は変更できません。 文字列をスペースでは実際には空の文字列ですが、非ゼロの大きさを有しています。したがって、len() または not 演算子を使用して空の文字列をチェックすると、文字列内の文字としてスペースがカウントされるため、スペースのある文字列は空の文字列としてカウントされません。
Pythonで空の文字列をチェックするには、次のいずれかの方法を使用できます。
Pythonのnot演算子は、スペースだけの文字列が空でないことを確認します。これは実際にはTrueであってはなりません。
# app.py
strA = ""
# checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Check if the strA is empty : The string is empty
文字列は空です。文字列にスペースを入れていないことに注意してください。そのため、空として返されます。文字列にスペースを入れると、空としてカウントされず、not演算子 はFalseを返します 。
# app.py
strA = " "
# checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Check if the strA with space is empty : No it is not empty
Pythonで空の文字列をチェックするには、len()関数を使用します。0が返された場合は、文字列が空であることを意味します。そうでなければ、そうではありません。
したがって、文字列に何かがある場合、それは空でない文字列としてカウントされます。それ以外の場合は、空の文字列です。
strA = ""
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここで、条件が0を返すためにFalseに なる場合 、それ以外の条件はTrueになり、文字列は空になります。
文字列に空白が含まれている場合、空の文字列としてカウントされません。
strA = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is not empty
文字列内のスペースは文字としてカウントされます。そのため、空をチェックするとFalseが返されます。
Pythonで純粋な空の文字列をチェックするには、len() + string.strip()メソッドを使用します。string.strip()メソッドは、文字列から空白を除去します。したがって、スペースが含まれている場合、strip()関数は削除し、文字列が空であるかどうか、またはlen()関数を使用していないかどうかを確認します。
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここでは、文字列にいくつのスペースを追加しても、すべてのスペースが削除され、文字列の長さがチェックされます。0が返された場合は、文字列が空であることを意味します。そうでなければ、そうではありません。
str = " KRUNAL "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is not empty
この例では、文字列に特定の文字が含まれているため、文字列が空ではないことがはっきりとわかります。したがって、len()メソッドは文字列の長さを返し、if条件はTrueを返します。
string.isspace()文字列にスペースが含まれているか、いない場合は機能をチェック。文字列にスペースが含まれている場合は、Trueを返します。それ以外の場合は、Falseを返します。
私たちは、の組み合わせを使用 文字列 と ないstring.isspace() 文字列が空であるかどうかにかかわらず、スペースのかどうかを確認する方法を。
このメソッドは、文字列に多くのスペースが含まれている場合に計算のロード時間がかかるストリップ操作を実行する必要があるため、strip()メソッドよりも効率的です。
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(str and not str.isspace()):
print("The string is not empty")
else:
print("The string is empty")
Check if the string is empty : The string is empty
ここでは、isspace()関数の負の条件を and演算子でチェックしています。
条件の1つがFalseになった場合、「and演算子」により、条件がFalse を返し、それ 以外の 場合は条件が実行されます。
これは、Pythonで純粋な空の文字列をチェックするためのより良い方法です。
このチュートリアルは以上です。
リンク:https://appdividend.com/2020/11/17/how-to-check-if-string-is-empty-or-not-in-python/