Python の strip() 関数の使い方
Python の `strip()` 関数は、文字列の先頭と末尾から指定した文字(デフォルトではスペース文字)を削除します。
strip 関数の構文は次のとおりです。
string.strip([chars])
パラメータの説明
- 指定する文字を除去する. 文字を指定しない場合は、デフォルトで文字列の先頭のスペース文字と末尾のスペース文字を除去します.
例文
string = " hello world "
print(string.strip()) # 输出: "hello world"
string = "---hello world---"
print(string.strip("-")) # 输出: "hello world"
string = "123hello world321"
print(string.strip("123")) # 输出: "hello world"
上記の例では、最初の例は文字列stringの両端の空白文字を取り除いています。 2番目の例は文字列stringの両端の連続した-文字を取り除いています。 3番目の例は文字列stringの両端の連続した1、2、3文字を取り除いています。