編寫一個 Python 程式,從給定的字串中去除一個特定長度的子字串


我們需要編寫一個 Python 程式,該程式將從給定的字串中刪除一個特定子字串

演算法

Step 1: Define a string.
Step 2: Use the replace function to remove the substring from the given string.

示例程式碼

線上演示

original_string = "C++ is a object oriented programming language"

modified_string = original_string.replace("object oriented", "")

print(modified_string)

輸出

C++ is a  programming language

說明

內建 Python replace() 函式採用以下引數

  • Oldstring:您要刪除的字串
  • Newstring:您要在 oldstring 位置替換的新字串
  • Count:可選。您要將 oldstring 替換為 newstring 的次數

更新日期: 16-03-2021

127 次瀏覽

開啟您的 職業

透過完成課程獲得認證

立即開始
廣告