Python|从给定字符串的开头删除 n 个字符的方法
给定一个字符串和一个数字“n”,任务是从字符串的开头删除一个长度为“n”的字符串。让我们用一些方法来解决给定的任务。方法#1:使用朴素方法
# Python3 code to demonstrate
# how to remove 'n' characters from starting
# of a string
# Initialising string
ini_string1 = 'garg_akshat'
# Initialising number of characters to be removed
n = 5
# Printing initial string
print ("Initial String", ini_string1)
# Removing n characters from string using
# Naive method
res = ''
for i in range(0, len(ini_string1)):
if i>= n:
res = res + ini_string1[i]
# Printing resultant string
print ("Resultant String", res)
输出:
Initial String garg_akshat
Resultant String akshat
方法 #2:使用replace()
# Python3 code to demonstrate
# how to remove 'n' characters from starting
# of a string
# Initialising string
ini_string1 = 'garg_akshat'
# Initialising number of characters to be removed
n = 5
# Printing initial string
print ("Initial String", ini_string1)
# Removing n characters from string using
# replace() function
res = ini_string1.replace(ini_string1[:5], '', 1)
# Printing resultant string
print ("Resultant String", res)
输出:
Initial String garg_akshat
Resultant String akshat
方法#3:字符串切片
# Python3 code to demonstrate
# how to remove 'n' characters from starting
# of a string
# Initialising string
ini_string1 = 'gargakshat123'
# Initialising number of characters to be removed
n = 5
# Printing initial string
print ("Initial String", ini_string1)
# Removing n characters from a string
# This argument count length from zero
# So length to be stripped is passed n-1
res = ini_string1[4:]
# Printing resultant string
print ("Resultant String", res)
输出:
Initial String gargakshat123
Resultant String akshat123