Python 中的strip()函數

strip()函數是 Python 的預定義庫函數。它用於通過移除傳遞給 strip()函數的前導和尾隨空格、字元和符號來返回原始字元串的副本。換句話說,是 Python 字元串 strip()函數通過為 strip()函數指定一組字元作為參數來刪除字元串左右兩端的字元。默認情況下,如果沒有參數傳遞給 strip()函數,它會從開始和結束字元串中刪除空格。

句法


strip( 'characters' )

參數:

  1. strip(‘ chars ‘):strip()參數是可選參數。因此,如果程序員沒有向 strip()函數傳遞任何參數,它將從字元串中刪除開始和結束空格。
  2. 如果給定參數的集合被傳遞給 strip()函數,它將從原始字元串中移除字元或符號。

返回值:通過從原始字元串中刪除字符集或空格,返回原始字元串的副本。

使用 strip()函數從給定字元串中刪除字元或符號

讓我們考慮一個在 Python 中通過移除給定字元串的前導或尾隨字元來執行 strip()函數的示例。

剝離


strinput = "  $$$$$  No. 1 Welcome to JAVATPOINT!! No. 1 $$$ "   
# use strip() function to remove the set of characters
res = strinput.strip ( ' $No. 10 !' ) # store result into res variable
print ( " Given string is: ", strinput)
print ( " After removing the set of characters: ", res) 

str3 = ' 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1 '
str4 = str3\. strip ( '1' )
print (" \n  Given string is ", str3)
print (" Stripping 1 from both ends of the string using strip ('1') function ", str4)

# define new string
str5 = '++++++Python Programming Tutorial****** $$$$$'
print ("\n Given string is = ", str5)
# use strip function to remove the symbols from both ends
str6 = str5\. strip ( ' $*+' )
print (" Stripping the '+', '*' and '$' symbols on both sides of the string is = ", str6)

輸出

Given string is:    $$$$$  No. 1 Welcome to JAVATPOINT!! No. 1 $$$ 
 After removing the set of characters:  Welcome to JAVATPOINT

 Given string is   1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1
 Stripping 1 from both ends of the string using strip ('1') function   1 11 111 111 1111 Learn Python 
Programming Tutorial 1111 111 11 1

 Given string is =  ++++++Python Programming Tutorial****** $$$$$
 Stripping the '+', '*' and '$' symbols on both sides of the string is =  Python Programming Tutorial

使用 strip()函數刪除給定字元串中的空白

讓我們考慮一個在 Python 中通過移除給定字元串的前導或尾隨空白來執行 strip()函數的示例。

條紋 2.py


str1 = '   Welcome to the World!              '
# print the original string
print (' Given string is: ', str1)

# use strip() function to remove whitespace
str2 = str1.strip()
print(" After removing whitespaces from an original string: ", str2)

str3 = '             Learn    Python    programming          '
# print the original string
print (' Given string is: ', str3)

# use strip() function to remove whitespace
str4 = str3.strip()
print(" After removing whitespaces from an original string: ", str4)

輸出

Given string is:     Welcome to the World!
 After removing whitespaces from an original string:  Welcome to the World!
 Given string is:               Learn    Python    programming
 After removing whitespaces from an original string:  Learn    Python    programming

在上面的程序中,我們使用 strip()函數從給定字元串的開頭和結尾移除空格,但它不會移除字元串之間的空格。

使用 strip()函數從用戶處獲取任何字元串並刪除任何字元或符號的程序

程序


str1 = input( " Enter the string ") # take a string 
print (" Your string is ", str1) 
ch = input (" Enter any character or symbol that you don't want to see in the string ")
res = str1.strip (ch) 
# print the string after using a strip() method
print (" After performing the strip() function, \n Your string is ", res)

輸出

Enter the string *** $$ Welcome to JavaTpoint. Learn Python programming !!! &&
 Your string is  *** $$ Welcome to JavaTpoint. Learn Python programming !!! &&
 Enter any character or symbol that you don't want to see in the string * $ Wel ! &
 After performing the strip() function, 
 Your string is  come to JavaTpoint. Learn Python programming

為什麼使用 Python strip()函數?

下面是使用 python strip 函數的原因:

  1. 它有助於根據傳遞給 strip()函數的字元從原始字元串的開頭和結尾移除字元。
  2. 如果用戶沒有向 strip 函數傳遞任何字元,默認情況下,它只刪除字元串兩端的空白。
  3. 如果原始字元串的開頭或結尾沒有空格,它將返回原始字元串而不進行修改。
  4. 如果傳遞的字元與原始字元串不匹配,strip 函數將返回原始字元串。

結論:

在上面的主題中,我們已經了解了 Python 庫的 strip()函數。這是一個 strip()函數,用於從原始字元串的開頭和結尾刪除字元或空格。但是,如果用戶沒有向 strip()函數傳遞任何字元,它只會從主字元串的前導和尾部刪除空格。


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/293633.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-26 13:14
下一篇 2024-12-26 13:14

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論