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 函数和列表推导式。 您可以根据您的个人需求和编码偏好从每个选项中进行选择,每个选项都提供了解决该问题的方案。
在处理大型字符串或极长的所需长度时,务必牢记性能影响,因为某些方法可能比其他方法更有效。
广告