Python 替换字符串中的每个第N个字符
在Python中,替换字符串的第n个字符是指将特定索引位置上的字符替换为新字符的过程。可以通过将字符串转换为可变数据类型(如列表),替换所需字符,并将其转换回字符串来实现。它允许根据位置修改字符串中的特定字符。
示例:
假设我们已经输入了一个字符串K,以及Nth索引值。我们现在将使用以上方法在字符串中用给定的K符号替换每个第N个索引。
输入:
inputString = "hello tutorialspoint python users"
k = '#'
N = 4
输出
hell# tu#ori#lsp#int#pyt#on #ser#
在上面的输入字符串中,每个索引为4的字符被替换为K,即在字符串中替换为#。
使用的方法
以下是实现此任务的各种方法:
- 使用for循环和enumerate()函数
-
使用生成器表达式,join()和enumerate()函数
-
使用list()函数
语法
enumerate(iterable, start=0)
参数
-
iterable- 它可以是任何支持迭代的序列/对象/可迭代对象
-
start- enumerate()从此值开始计数。如果未指定start,则使用值0。
join(): join() 是Python中的字符串函数,用于将由字符串分隔符分隔的序列元素连接起来。此函数连接序列元素以转换为字符串。
separator.join(iterable)
list() 函数: 将序列或可迭代对象转换为列表。
步骤
下面是执行所需任务的算法/步骤 –
- 创建一个变量来存储 输入字符串 。
-
打印输入字符串。
-
创建另一个变量来存储 要替换的输入 k 符号 。
-
创建另一个变量来存储 输入的 Nth 索引 。
-
初始化一个空字符串来存储结果字符串。
-
使用 for 循环 遍历输入字符串的索引和元素,使用 enumerate() 函数。
-
使用 if 条件 语句检查当前索引是否可以被 Nth 索引整除,如果不等于 0。
-
如果条件 为真 ,则将 k 符号添加/连接到结果字符串中,用于每个 Nth 索引。
-
否则,将元素添加到结果字符串中。
-
替换每个 Nth 索引为 k 符号后,打印结果字符串。
示例 1: 使用 for 循环和 enumerate() 函数
以下程序使用 for 循环和 enumerate() 函数返回一个字符串,其中每个 Nth 索引都被 k 符号替换。
# input string
inputString = "hello tutorialspoint python users"
# printing input string
print("Input string: ", inputString)
# input K symbol
k = '#'
# input Nth index
N = 4
# empty string for storing resultant string
resultantStr = ''
# traversing through the index, element of input string using enumerate() function
for index, element in enumerate(inputString):
# checking whether the current index is divisible by the Nth index and it is not equal to 0
if index % N == 0 and index != 0:
# adding the k symbol to the resultant string for every Nth index if the condition is true
resultantStr = resultantStr + k
else:
# else adding the element to the resultant string
resultantStr = resultantStr + element
# printing resultant string
print("Resultant string after replacing every 4th index with #:\n", resultantStr)
输出
Input string: hello tutorialspoint python users
Resultant string after replacing every 4th index with #:
hell# tu#ori#lsp#int#pyt#on #ser#
示例 2:使用生成器表达式、join()和enumerate()函数
下面的程序使用生成器表达式、join()和enumerate()函数,在每个第N个索引处用k符号替换后返回一个字符串 –
# input string
inputString = "hello tutorialspoint python users"
# printing input string
print("Input string: ", inputString)
# input K symbol
k = '#'
# input Nth index
N = 4
# Replacing Nth character with k
resultantStr = ''.join(e if index % N or index == 0 else k for index,
e in enumerate(inputString))
# printing resultant string
print("Resultant string after replacing every 4th index with #:\n", resultantStr)
输出
Input string: hello tutorialspoint python users
Resultant string after replacing every 4th index with #:
hell# tu#ori#lsp#int#pyt#on #ser#
示例3:使用list()函数
以下程序在使用list()函数后,会将每个第N个索引替换为k符号后返回字符串 –
# input string
inputString = "hello tutorialspoint python users"
# printing input string
print("Input string: ", inputString)
# input K symbol
k = '#'
# input Nth index
N = 4
# converting the input string into a list of characters
charsList = list(inputString)
# empty string for storing resultant string
resultantStr = ""
# traversing till the length of the characters list
for index in range(0, len(charsList)):
# checking whether the current index is not equal to 0 and it is divisible by Nth index
if(index != 0 and index % N == 0):
# adding the k symbol to the resultant string for every Nth index if the condition is true
resultantStr += k
else:
# else adding the value at the current index to the resultant string
resultantStr += inputString[index]
# printing resultant string
print("Resultant string after replacing every 4th index with #:\n", resultantStr)
输出
Input string: hello tutorialspoint python users
Resultant string after replacing every 4th index with #:
hell# tu#ori#lsp#int#pyt#on #ser#
结论
在Python中替换字符串的第n个字符涉及将特定索引处的字符替换为新字符。这可以通过将字符串转换为可变数据类型(如列表),修改所需的字符,然后将其转换回字符串来完成。这种方法允许根据其位置有针对性地修改字符串,提供了灵活性和控制力。通过理解并应用这种技术,人们可以在Python编程中有效地操纵字符串。