使用 Python 從字串形式給定的數字中刪除前導零


在本文中,我們將學習一個 Python 程式,用於從作為字串給定的數字中刪除前導零。

假設我們已經以字串格式獲取了一個數字。我們現在將使用下面給出的方法刪除所有前導零(數字開頭處的零)。

使用的方法

以下是完成此任務的各種方法:

  • 使用 For 迴圈和 remove() 函式

  • 使用正則表示式

  • 使用 int() 函式

方法 1:使用 For 迴圈和 remove() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個函式 deleteLeadingZeros(),該函式從作為字串傳遞給函式的數字中刪除前導零。

  • 使用 for 迴圈,使用 len() 函式遍歷字串的長度。

  • len() 函式 - len() 方法返回物件中的專案數。當物件是字串時,len() 函式返回字串中的字元數。

  • 將 if 條件語句與 != 運算子一起使用,以檢查字串中當前字元是否不為 0。

  • 使用切片獲取前導零之後字串的剩餘字元。

  • 刪除輸入字串中所有前導 0 後,返回結果字串。

  • 如果未找到前導 0,則返回 0。

  • 建立一個變數來儲存作為字串傳遞的輸入數字。

  • 透過將輸入字串傳遞給它來呼叫上面定義的 deleteLeadingZeros() 函式,以獲取刪除前導零後的結果字串。

  • 以同樣的方式檢查其他沒有前導零的字串。

示例

以下程式返回一個字串,該字串使用 For 迴圈和 remove() 函式從作為字串傳遞的數字中刪除所有前導零:

# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # traversing through the length of the string
   for k in range(len(inputString)):
      # checking whether the current character of a string is not 0
      if inputString[k] != '0':
         # getting the remaining string using slicing
         outputString= inputString[k::]
         # returning resultant string after removing leading 0s
         return outputString
   # returning 0 if there are no leading 0s found in the entire string
   return "0"
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))
# checking it for other strings having no leading 0s
inputString = "256"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

輸出

執行後,上述程式將生成以下輸出:

Given String is: 0002056
After Removing Leading Zeros: 2056
Given String is: 256
After Removing Leading Zeros: 256

方法 2:使用正則表示式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 import 關鍵字匯入正則表示式 (re) 模組。

  • 建立一個函式 deleteLeadingZeros(),該函式從作為字串傳遞給函式的數字中刪除前導零。

  • 建立一個變數來儲存用於從輸入字串中刪除前導零的正則表示式模式。

  • 使用 sub() 函式將匹配的正則表示式模式替換為空字串。

  • sub() 函式(返回一個字串,其中給定模式的所有匹配出現都被替換字串替換)。

  • 刪除輸入字串中所有前導 0 後,列印結果字串。

示例

以下程式返回一個字串,該字串使用正則表示式從作為字串傳遞的數字中刪除所有前導零:

# importing re module
import re
# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # regex pattern for removing leading zeros from an input string
   regexPattern = "^0+(?!$)"
   # Replace the matched regex pattern with an empty string
   outputString = re.sub(regexPattern, "", inputString)
   # returning output string after removing leading 0s
   return outputString
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

輸出

執行後,上述程式將生成以下輸出:

Given String is: 0002056
After Removing Leading Zeros: 2056

方法 3:使用 int() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個函式 deleteLeadingZeros(),該函式從作為字串傳遞給函式的數字中刪除前導零。

  • 使用 int() 函式(從給定物件返回整數)將輸入字串轉換為整數。此函式刪除所有前導零。

  • 刪除輸入字串中所有前導 0 後,返回結果數字。

示例

以下程式返回一個數字,該數字使用 int() 函式從作為字串傳遞的數字中刪除所有前導零:

# creating a function that removes the leading zeros
# from a number passed as a string to the function
def deleteLeadingZeros(inputString):
   # converting the input string to an integer removes all the leading zeros
   result = int(inputString)
   # returning the resultant number after removing leading zeros
   return result
# input number as a string
inputString = "0002056"
print("Given String is:", inputString)
# calling the deleteLeadingZeros() function by passing the input string to it
print("After Removing Leading Zeros:", deleteLeadingZeros(inputString))

輸出

執行後,上述程式將生成以下輸出:

Given String is: 0002056
After Removing Leading Zeros: 2056

結論

在本文中,我們學習瞭如何使用三種不同的方法從作為字串給定的數字中刪除前導零。我們學習瞭如何使用切片獲取可迭代物件(如字串、列表或元組)的子集。我們還學習瞭如何利用正則表示式模組將一種模式替換為另一種模式。

更新於: 2023年1月31日

12K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告