สตริง หมายถึงอาร์เรย์ของอักขระ ดังนั้นที่อยู่เริ่มต้นคือ 0 จากนั้นเราสามารถรับดัชนีของอักขระทุกตัวได้อย่างง่ายดาย เราต้องป้อนหมายเลขดัชนีนั้น แล้วเอาองค์ประกอบนั้นออก ดังนั้นแยกสตริงออกเป็นสองสตริงย่อย และสองส่วนควรเป็นอักขระก่อนหน้าตัวที่ n และอีกส่วนหลังอักขระที่จัดทำดัชนี ซึ่งรวมสองสตริงนี้เข้าด้วยกัน
ตัวอย่าง
Input: python n-th indexed: 3 Output: pyton
คำอธิบาย
อัลกอริทึม
Step 1: Input a string. Step 2: input the index p at the removed character. Step 3: characters before the p-th indexed is stored in a variable X. Step 4: Character, after the n-th indexed, is stored in a variable Y. Step 5: Returning string after removing n-th indexed character.
โค้ดตัวอย่าง
# Removing n-th indexed character from a string
def removechar(str1, n):
# Characters before the i-th indexed is stored in a variable x
x = str1[ : n]
# Characters after the nth indexed is stored in a variable y
y = str1[n + 1: ]
# Returning string after removing the nth indexed character.
return x + y
# Driver Code
if __name__ == '__main__':
str1 = input("Enter a string ::")
n = int(input("Enter the n-th index ::"))
# Print the new string
print("The new string is ::")
print(removechar(str1, n))
ผลลัพธ์
Enter a string:: python Enter the n-th index ::3 The new string is :: pyton