Pythonで文字列が空文字であるかを判定する

環境
Python 3.9.13
Windows 11 Pro 21H2 64bit
PyCharm 2022.2.1 (Community Edition)

構文
方法1
文字列変数名 = 値
文字列変数 == “"

方法2
文字列変数名 = 値
not 文字列変数名
文字列が空文字であるかを判定します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
strA = ""
result = (strA == "")
print("文字列が空文字であるかを判定する方法1")
print(result )
strB = ""
result2 = (not strB)
print("文字列が空文字であるかを判定する方法2")
print(result2)
strA = "" result = (strA == "") print("文字列が空文字であるかを判定する方法1") print(result ) strB = "" result2 = (not strB) print("文字列が空文字であるかを判定する方法2") print(result2)
strA = ""
result = (strA == "")
print("文字列が空文字であるかを判定する方法1")
print(result )

strB = ""
result2 = (not strB)
print("文字列が空文字であるかを判定する方法2")
print(result2)

実行結果
文字列が空文字であるかを判定する方法1
True
文字列が空文字であるかを判定する方法2
True

Python

Posted by arkgame