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个元素的字符字符串。

更新于:2023年8月18日

浏览量:143

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.