Python - 重複字串直到長度為 K


Python 被世界各地不同的人們用於各種目的,例如 Web 開發、機器學習、資料科學以及執行不同的自動化流程。 在本文中,我們將學習如何透過反覆重複同一個字串來增加字串的長度,直到達到所需的長度。

重複字串的不同方法

字串乘法

在這種方法中,我們只需透過乘法運算即可達到所需的長度。 我們將提供一個特定的值,並且字串將被乘以以達到特定數量的字元。 此方法的語法如下所示

def repeat_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    whole_string = length // len(data)  # The number of times the whole string will be written is found
    extra_characters = length % len(data)    # The number of times some additional characters will be added to achieve the number of characters is found
    
    final_string = data * whole_string + data[:extra_characters] # A new string is created by repeating the whole string and adding some additional characters to achieve the required length
    
    return final_string

示例

上述方法的示例如下所示

def repeat_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    whole_string = length // len(data)  # The number of times the whole string will be written is found
    extra_characters = length % len(data)    # The number of times some additional characters will be added to achieve the number of characters is found
    
    final_string = data * whole_string + data[:extra_characters] # A new string is created by repeating the whole string and adding some additional characters to achieve the required length
    
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25  # The desired number of characters are specified

new_string = repeat_string(old_string, final_length) # The function is repeat_string is run
print(new_string)

輸出

上述程式碼的輸出如下所示

ExampleExampleExampleExam

While 迴圈

在這種方法中,while 迴圈將持續執行字串,直到達到所需的長度並且語句變為真。 上述方法的語法如下所示

def whole_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    final_string = "" # A new empty string is created which have the repeated string
    
    while len(final_string) < length: # The Condition is provided to the loop to achieve the desired length 
        final_string += data
    
    final_string = final_string[:length]  # Remove all the undesired elements and make the string of the required length
    
    return final_string

示例

上述方法的示例如下所示

def whole_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    final_string = "" # A new empty string is created which have the repeated string
    
    while len(final_string) < length: # The Condition is provided to the loop to achieve the desired length 
        final_string += data
    
    final_string = final_string[:length]  # Remove all the undesired elements and make the string of the required length
    
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25 # The desired length of the string is specified

new_string = whole_string(old_string, final_length) # The function whole_string is run 
print(new_string)  # The output of the desired length is displayed

輸出

該方法的輸出如下所示

ExampleExampleExampleExam

Itertools

Itertools 用於有效地檢查字串中的每個元素。 我們將使用 itertools 庫的其中一個函式來達到所需的字串長度,並且為了停止字串的複製,一旦達到所需的長度,我們將使用切片函式。 此方法的語法如下所示

import itertools  # Do not forget to import itertools or else error might occur

def whole_string(data, length): # The input of the string is given along with the desired length of the string
    final_string = ''.join(itertools.islice(itertools.cycle(data), length)) #The itertools.cycle will help to repeat the string and the itertools.slice will stop the repeatation once the desired length is achieved 
    return final_string

示例

上述方法的示例如下所示

import itertools  # Do not forget to import itertools or else error might occur

def whole_string(data, length): # The input of the string is given along with the desired length of the string
    final_string = ''.join(itertools.islice(itertools.cycle(data), length)) #The itertools.cycle will help to repeat the string and the itertools.slice will stop the repeatation once the desired length is achieved 
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25 # The desired length of the string is specified

new_string = whole_string(old_string, final_length) # The function whole_string is run
print(new_string)  # The output of the desired length is displayed

輸出

上述程式碼的輸出如下所示

ExampleExampleExampleExam

列表推導式

此方法將檢查字串中的每個元素,然後建立一個新的字串,其中元素將持續重複直到達到所需的長度。 此方法的語法如下所示

def whole_string(data, length): # The input of the string is given along with the required length of the string
    final_string = [char for char in data for _ in range(length // len(data) + 1)] # Each element of the string is checked and then they are repeated until desired length is achieved
    updated_string = ''.join(final_string[:length]) # Once the desired length is achieved the elements are joined together into 1
    
    return updated_string

示例

上述方法的示例如下所示

def whole_string(data, length): # The input of the string is given along with the required length of the string
    final_string = [char for char in data for _ in range(length // len(data) + 1)] # Each element of the string is checked and then they are repeated until desired length is achieved
    updated_string = ''.join(final_string[:length]) # Once the desired length is achieved the elements are joined together into 1
    
    return updated_string
old_string = "Example"  #The input of the string is given
final_length = 25 # The desired length of the string is provided

new_string = whole_string(old_string, final_length) # The function whole_string is run
print(new_string) # The output having the desired length is displayed

輸出

示例的輸出如下所示

EEEExxxxaaaammmmpppplllle

結論

在 Python 中,通常會重複一個字串,直到它達到一定的長度。 在本文中,我們探討了四種不同的方法來實現這一點:字串乘法、while 迴圈、使用 itertools 函式和列表推導式。 您可以根據您的個人需求和編碼偏好從每個選項中進行選擇,每個選項都提供瞭解決該問題的方案。

在處理大型字串或極長的所需長度時,務必牢記效能影響,因為某些方法可能比其他方法更有效。

更新於: 2023年8月1日

91 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.