Python の strip() 関数の使用方法

Pythonにおけるstrip()は、文字列の先頭と末尾から指定した文字(デフォルトは空白文字)を削除する文字列メソッドです。

ご使用方法:

  1. 文字列の前後にある空白文字を削除する:
string = "  hello world  "
result = string.strip()
print(result)  # 输出: "hello world"
  1. 文字列から指定文字の頭と末端を削除する
string = "!!!hello world!!!"
result = string.strip("!")
print(result)  # 输出: "hello world"
  1. 文字列の先頭と末尾から、複数の指定した文字を削除する。
string = "!!!hello world!!!"
result = string.strip("! ")
print(result)  # 输出: "hello world"

注意:

  1. strip()メソッドは、新しい文字列を返します。元の文字列は変更されません。
  2. パラメーターを指定しない場合、デフォルトでは先頭と末尾の空白文字を削除します。
bannerAds