logo

Python で文字列が空かどうかを確認する方法

Python 文字列は不変のままです。つまり、実行する操作に基づいて文字列を変更できます。 Python の最も重要な機能は文字列操作です。文字列の変更には、文字列のスライス手法、要素のループ、文字列メソッドなどの多くの方法を使用できます。スペースを含む文字列は事実上、長さがゼロではない空の文字列であることを理解することが重要です。この問題と考えられる解決策については、このチュートリアルで説明します。その結果、使用すると、 のみ() または ' ない ' 演算子を使用して空の文字列をチェックすると、実際にはスペースが文字列の 1 文字としてカウントされるため、スペースを含む文字列は空の文字列としてカウントされません。

Python では、以下で説明する基本的な方法の 1 つを使用して、空の文字列をチェックできます。

Javaのランダム値ジェネレーター
  1. not演算子の使用
  2. len()関数の使用
  3. not + string.isspace() を使用する
  4. len() + string.strip() の使用
  5. および + string.strip() の使用
  6. __eq__ の使用

not 演算子の使用

ない オペレーターはオペレーターと同じ仕事を実行します のみ() 関数。 Python では、空の文字列は実際に False と同じです。の ない 演算子を使用して、文字列が実際に空かどうかを判断できます。の ない Python での操作により、文字列にスペースが含まれている場合に、文字列が空の文字列として解釈されなくなります。

この例では、string1 と string2 という 2 つの型の文字列を使用しました。 string2 には空白が含まれており、string1 は空の文字列です。次に、「if else」条件を使用して、指定された文字列が文字列内にない場合に空であるかどうかを確認しました。ただし、空ではありません。 not 演算子では空白は空の文字列として扱われないため、2 番目の入力文字列の場合、出力は空の文字列になりません。最後に、結果が出力されました。

コード:

 #taking an empty string and a string with spaces only string1 = '' string2 = ' ' if not string1: print(f'string, string1 = '{string1}', with no spaces is empty') else: print(f'string, string1 = '{string1}', with no spaces is not empty') if not string2: print(f'string, string2 = '{string2}', with spaces is empty') else: print(f'string, string2 = '{string2}', with spaces is not empty') 

出力:

 string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is not empty 

len() メソッドの使用

Pythonのものを使用します のみ() 文字列の長さを決定する関数。次に、文字列の長さが 0 の場合、文字列は空白になります。それ以外の場合はそうではありません。を使用するときは、 のみ() Python の手法では、文字列にスペースが含まれている場合、その文字列は実際には空の文字列とは見なされません。

この例では、string1 と string2 という 2 つの型の文字列を使用しました。 string2 には空白が含まれており、string1 は空の文字列です。次に、Python の len() 関数を使用して各文字列の長さが計算されました。次に、「if-else」ループを使用して、文字列の長さがゼロに等しいかどうかを確認しました。ゼロの場合、条件は文字列が空であることを出力し、その場合、出力文字列は空ではありません。文字列内の空白は空とはみなされません。 のみ() 、空ではない文字列になります。最後に、両方の文字列の結果を出力しました。

コード:

 #taking an empty string and a string with spaces only string1 = '' string2 = ' ' length1 = len(string1) length2 = len(string2) if length1 == 0: print(f'string, string1 = '{string1}', with no spaces is empty') else: print(f'string, string1 = '{string1}', with no spaces is not empty') if length2 == 0: print(f'string, string2 = '{string2}', with spaces is empty') else: print(f'string, string2 = '{string2}', with spaces is not empty') 

出力:

Pythonプログラミングの演算子
 string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is not empty 

not Operator + str.isspace() を使用する

と同じ方法でストリップ内の空白をチェックします。 ストリップ() 関数は行います。しかし、 ストリップ() 関数は逆にすると時間がかかります str. isspace() これは、strip() がストリップ操作を実行する必要があり、これには多くの計算作業が必要となるためです。

この例では、string1 と string2 という 2 つの型の文字列を使用しました。 string2 には空白が含まれており、string1 は空の文字列です。 「if-else」条件が使用されています。私たちが使用したのは、 isspace() if else 条件内のメソッド。すべての文字列スペースをチェックします。最後に、出力が表示され、両方の文字列が空であることがわかります。

コード:

 #taking an empty string and a string with spaces only string1 = '' string2 = ' ' if string1 and not string1.isspace(): print(f'string, string1 = '{string1}', with no spaces is empty') else: print(f'string, string1 = '{string1}', with no spaces is not empty') if string2 and not string2.isspace(): print(f'string, string2 = '{string2}', with spaces is empty') else: print(f'string, string2 = '{string2}', with spaces is not empty') 

出力:

 string, string1 = '', with no spaces is not empty string, string2 = ' ', with spaces is not empty 

len() + string.strip() の使用

Python では、 len() + string.strip() 完全に空の文字列を検証する手法。文字列内の空白は次のコマンドを使用して削除されます。 文字列.ストリップ() 方法。文字列にスペースがある場合、 ストリップ() メソッドはそれを削除し、 のみ() 関数は文字列が空かどうかをチェックします。

文字列に入れるスペースの数に関係なく、すべてのスペースが取り除かれ、文字列の長さが検証されます。 0 を返す場合、文字列は空です。そうでなければ、そうではありません。

コード:

メインメソッドJava
 #taking an empty string and a string with spaces only string1 = '' string2 = ' ' if len(string1.strip()): print(f'string, string1 = '{string1}', with no spaces is not empty') else: print(f'string, string1 = '{string1}', with no spaces is empty') if len(string2.strip()): print(f'string, string2 = '{string2}', with spaces is not empty') else: print(f'string, string2 = '{string2}', with spaces is empty') 

出力:

 string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is empty 

「and」演算子+strip()関数の使用

文字列内の空白が常に空の文字列として解釈されるわけではないことに気づきました。したがって、を使用するときは、 ストリップ() 文字列が空かどうかを確認する関数ですが、実際に空白もチェックできます。

この例では、2 つの入力文字列 string1 と string2 を使用しました。 string2 には空白が含まれており、string1 は空の文字列です。次に、if-else 条件を使用しました。 ストリップ() 文字列が空かどうかを確認する関数。条件が False の場合、文字列は空であり、それ以外の場合はブロックが実行されます。このメソッドでは、空白も空の文字列とみなされます。最後に、結果が出力されました。

xdの意味

コード:

 #input empty with and without spaces string s = '' str = ' ' if string1 and string1.strip(): print(f'string, string1 = '{string1}', with no spaces is not empty') else: print(f'string, string1 = '{string1}', with no spaces is empty') if string2 and string2.strip(): print(f'string, string2 = '{string2}', with spaces is not empty') else: print(f'string, string2 = '{string2}', with spaces is empty') 

出力:

 string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is empty 

Strip() 関数の使用

文字列内の空白が常に空の文字列として解釈されるわけではないことに気づきました。したがって、を使用するときは、 ストリップ() 文字列が空かどうかを確認する関数ですが、実際に空白もチェックできます。

二分木と二分探索木の違い

この例では、2 つの入力文字列 string1 と string2 を使用しました。 string1 には空白が含まれており、string2 は空の文字列です。 if else 条件を使用して文字列を削除し、空白を削除し、文字列が空になった場合は空の文字列を返します。それ以外の場合、文字列は空ではありません。最後に、結果が出力されました。

コード:

 #input empty with and without spaces string s = '' str = ' ' if string1.strip(): print(f'string, string1 = '{string1}', with no spaces is not empty') else: print(f'string, string1 = '{string1}', with no spaces is empty') if string2.strip(): print(f'string, string2 = '{string2}', with spaces is not empty') else: print(f'string, string2 = '{string2}', with spaces is empty') 

出力:

 string, string1 = '', with no spaces is empty string, string2 = ' ', with spaces is empty 

__eq__ メソッドの使用

ダンダーは、名前の前後に 2 つのアンダースコアがあるメソッドを識別します。の __eq__ メソッドを使用して空の文字列をチェックすることもできます。電話をかけるときは、 __eq__ Python の手法では、文字列にスペースが含まれている場合、その文字列は空の文字列とは見なされません。

この例では、2 つの入力文字列 string1 と string2 を使用しました。 string2 には空白が含まれており、string1 は空の文字列です。の __eq__ 関数が使用されています。 if-else 条件では、指定されたメソッドを使用して文字列が空白かどうかを確認しました。最後に、結果が出力されました。このアプローチでは、空白は空の文字列として扱われません。

コード:

 #taking an empty string and a string with spaces only string1 = '' string2 = ' ' if ''.__eq__(string1): print(f'string, string1 = '{string1}', with no spaces is empty') else: print(f'string, string1 = '{string1}', with no spaces is not empty') if ''.__eq__(string2): print(f'string, string1 = '{string2}', with no spaces is empty') else: print(f'string, string1 = '{string2}', with no spaces is not empty') 

出力:

 string, string1 = '', with no spaces is empty string, string1 = ' ', with no spaces is not empty