Python程式將數字移動到字串末尾


在本文中,我們將學習一個Python程式,用於將數字移動到字串的末尾。

使用的方法

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

  • 使用isdigit()和for迴圈

  • 使用isdigit()和join()函式

  • 不使用任何內建函式

  • 使用isnumeric()函式

  • 使用isalpha()函式

示例

假設我們已經獲取了一個輸入字串。我們現在將使用上述方法將輸入字串中包含的所有數字移動到字串的末尾。

輸入

inputString = 'hello5691 tutorialspoint1342'

輸出

Resultant string after adding digits at the end:
hello tutorialspoint56911342

這裡,輸入字串中包含的所有數字56911342都已移動到字串的末尾。

方法1:使用isdigit()和for迴圈

演算法(步驟)

以下是執行所需任務應遵循的演算法/步驟。

  • 建立一個變數來儲存輸入字串。

  • 建立一個空字串來儲存結果字串。

  • 建立另一個空字串來儲存所有數字。

  • 使用for迴圈遍歷輸入字串的每個字元。

  • isdigit()函式應用於當前字元,以使用if條件語句檢查當前字元是否為數字。

  • 如果條件為真,則將該當前字元新增到數字字串中。

  • 否則,將該字元新增到上述結果字串中。

  • 使用+運算子(字串連線)將數字新增到結果字串的末尾。

  • 在將數字新增到字串末尾後,列印結果字串。

示例

以下程式使用for迴圈和isdigit()函式返回在輸入字串末尾新增所有數字後的字串 -

# input string
inputString = 'hello5691 tutorialspoint1342'

# printing the input string
print("Input string: ", inputString)

# storing resultant string
resultantString = ''

# storing all digits
digits = ''

# traversing through each character of the input string
for c in inputString:
   
   # checking whether the current character is a digit or not
   if c.isdigit():
      
      # add that character to a digits string, if the condition is true
      digits += c
   else:
      
      # else add that character to the above resultant string
      resultantString += c

#concatenating/adding digits at the end of the resultant string
resultantString += digits

# printing resultant string after adding digits at the end
print("Resultant string after adding digits at the end:\n", resultantString)

輸出

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

Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342

方法2:使用isdigit()和join()函式

join()函式 - join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。

示例

以下程式使用isdigit()和join()函式返回在輸入字串末尾新增所有數字後的字串 -

# input string
inputString = 'hello5691 tutorialspoint1342'

# printing the input string
print("Input string: ", inputString)

#traversing through each character of a string and getting all digits
digits = ''.join(c for c in inputString if c.isdigit())

# getting all other characters(not digits)
resultantString = ''.join(c for c in inputString if not c.isdigit())

# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)

輸出

Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342

方法3:不使用任何內建函式。

示例

以下程式返回在輸入字串末尾新增所有數字後的字串,不使用任何內建函式 -

# input string
inputString = 'hello5691 tutorialspoint1342'

# printing the input string
print("Input string: ", inputString)

# storing all digits
digits_str = "0123456789"

# storing all characters except digits
resultantString = ''

# storing all digits of an input string
resultDigits = ''

# traversing through each character of a string
for c in inputString:
   
   # checking whether that current character is in the above digits string
   if c in digits_str:
      
      # adding that character to the resultDigits string
      resultDigits += c
   else:
      
      # else adding that character to the resultant string
      resultantString += c

# concatenating/adding digits at the end of the resultant string
resultantString += resultDigits
print("Resultant string after adding digits at the end:\n", resultantString)

輸出

Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342

方法4:使用isnumeric()函式

isnumeric()函式 - 如果字串中的所有字元都是數字(0-9),則返回True,否則返回False

示例

以下程式使用isnumeric()函式返回在輸入字串末尾新增所有數字後的字串 -

# input string
inputString = 'hello5691 tutorialspoint1342'

# printing the input string
print("Input string: ", inputString)

# storing all characters except digits
resultantString = ''

# storing all digits
digits = ''

# traversing through each character of an input string
for c in inputString:
   
   # checking whether the current character is a digit
   if c.isnumeric():
      
      # adding that digit to the above digits string
      digits += c
   else:
      
      # else adding that character to the resultantString
      resultantString += c

# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)

輸出

Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342

方法5:使用isalpha()函式

isalpha()函式 - 如果所有字元都是字母(a-z),則返回True,否則返回False

示例

以下程式使用isalpha()函式返回在輸入字串末尾新增所有數字後的字串 -

# input string
inputString = 'hello5691 tutorialspoint1342'

# printing the input string
print("Input string: ", inputString)

# storing all characters except digits
resultantString = ''

# storing all digits
digits = ''

# traversing through each character of a string
for c in inputString:
   if(c!=' '):
   
      # checking whether current character is an alphabet
      if c.isalpha():
         
         # adding that character to the resultantString
         resultantString += c
      else:
         
         # else adding that digit to the digits
         digits += c

# concatenating/adding digits at the end of the resultant string
resultantString += digits
print("Resultant string after adding digits at the end:\n", resultantString)

輸出

Input string: hello5691 tutorialspoint1342
Resultant string after adding digits at the end:
hello tutorialspoint56911342

結論

在本文中,我們介紹了5種將數字移動到字串末尾的不同方法。我們還透過不使用內建函式實現了相同的程式。

更新於: 2023年1月27日

696 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.