Python程式保留字串前N個元素並將剩餘元素替換為K
在本文中,我們將學習如何使用Python內建函式,例如len()、切片、replace()和ljust(),來保留字串的前N個元素並將剩餘元素替換為K。Python字串很容易建立,只需用引號括起來即可。Python對單引號和雙引號的處理方式相同。為變數賦值和建立字串非常簡單。
示例
假設我們已經獲取了輸入字串、N值和K值。現在,我們將保留輸入字串的前N個字元,並使用上述方法將其餘字元替換為給定的K字元。
輸入
inputString = 'tutorialspoint' input_N = 9 input_K = "#"
輸出
Retaining N{9} characters and replacing rest with '#': tutorials#####
在上述示例中,保留了輸入字串的前N(9)個字元tutorials,其餘字元被替換為輸入的K,即#符號。
使用*運算子、切片和len()函式
在這種方法中,我們將使用*運算子和內建函式,例如切片和len(),來保留字串的前N個元素,然後將剩餘元素替換為K。這裡,len()函式返回物件中的專案數。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立一個變數來儲存輸入字串。
列印輸入字串。
建立一個變數來儲存要保留的輸入N個元素。
建立一個變數來儲存要替換其餘元素的輸入字元K。
對字串的前N個字元進行切片以保留前N個元素。
將給定字元乘以剩餘元素的數量。
使用+運算子連線上述兩個字串。
列印保留輸入N個元素並將剩餘元素替換為輸入字元k後的結果字串。
示例
下面的程式使用*運算子、切片和len()函式返回保留輸入N個元素並將剩餘元素替換為輸入字元k後的字串。
# input string
inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
# Slicing till the input N index to retain the first N elements
# Multiplying the remaining number of elements with the replaced character
resultantStr = inputString[:input_N] + input_K * (len(inputString) - input_N)
# printing resultant string after retaining and replacing with the input character
print("Retaining N{9} characters and replacing rest with '#':", resultantStr)
輸出
執行上述程式將生成以下輸出:
Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#': tutorials#####
使用切片、ljust()和len()函式
在這種方法中,我們將使用Python內建函式,例如切片、ljust()和len(),來執行給定的任務。
語法
string.ljust(length, character)
這裡,ljust()函式用於使用給定字元作為填充字元來左對齊字串。空格是預設字元。
示例
下面的程式使用切片、ljust()和len()函式返回保留輸入N個元素並將剩餘元素替換為輸入字元k後的字串。
# input string
inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input the number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
# Slicing till the input N index to retain the first N elements
# Applying ljust() function to add remaining characters with the replaced character
resultantStr = inputString[:input_N].ljust(len(inputString), input_K)
# printing resultant string after retaining and replacing with the input character
print("Retaining N{9} characters and replacing rest with '#':", resultantStr)
輸出
執行上述程式將生成以下輸出:
Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#': tutorials#####
使用replace()函式
在這種方法中,我們將使用Python的replace函式來保留字串的前N個元素並將剩餘元素替換為K。
語法
string.replace(old, new, count)
這裡的replace()函式返回一個字串副本,該副本將所有舊子字串的出現替換為另一個新子字串。
演算法(步驟)
以下是執行所需任務的演算法/步驟。
建立函式replaceString(),透過接受輸入字串作為引數來保留字串的前N個元素並將剩餘元素替換為輸入K。
使用replace()函式將剩餘字元替換為給定的k字元。
返回保留並替換為輸入字元後的結果字串。
建立一個變數來儲存輸入字串。
列印輸入字串。
建立一個變數來儲存要保留的輸入N個元素。
建立一個變數來儲存要替換其餘元素的輸入字元K。
透過將輸入字串作為引數傳遞給它來呼叫上述定義的replaceString()函式。
示例
下面的程式使用replace()函式返回保留輸入N個元素並將剩餘元素替換為輸入字元k後的字串。
# creating a function that retains the first N Elements of a string and
# replace the remaining with K by accepting the input string as an argument
def replaceString(inputString):
# Replacing the remaining characters with the given k character
resultantStr = inputString.replace(
inputString[input_N:], input_K*len(inputString[input_N:]))
# returning resultant string after retaining and replacing with the input character
return str(resultantStr)
inputString = 'tutorialspoint'
# printing input string
print("Input string:", inputString)
# input number of elements to be retained
input_N = 9
# input character to be replaced with rest of other elements
input_K = "#"
print("Retaining N{9} characters and replacing rest with '#':")
# calling the above defined replaceString() function by passing the
# input string as an argument to it
print(replaceString(inputString))
輸出
執行上述程式將生成以下輸出:
Input string: tutorialspoint
Retaining N{9} characters and replacing rest with '#':
tutorials#####
結論
本文展示了三種不同的方法來保留字串的前N個元素並將剩餘元素替換為另一個字串K。我們學習瞭如何使用ljust()方法將元素新增到字串的末尾。此外,我們學習瞭如何使用*運算子將字元乘以n來建立包含n個元素的字元字串。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP