Sunday, 29 December 2024

Day 65 : Python Program to Remove the nth Index Character from a Non Empty String

 


def remove_nth_index_char(input_string, n):

    """

    Remove the character at the nth index from the input string.

Args:

        input_string (str): The string to process.

        n (int): The index of the character to remove.

Returns:

        str: A new string with the nth character removed.

    """

    if n < 0 or n >= len(input_string):

        raise ValueError("Index n is out of range.")

    return input_string[:n] + input_string[n+1:]

if __name__ == "__main__":

    input_string = input("Enter a non-empty string: ")

    try:

        n = int(input("Enter the index of the character to remove: "))

        result = remove_nth_index_char(input_string, n)

        print("String after removing the nth index character:", result)

    except ValueError as e:

        print("Error:", e)

#source code --> clcoding.com

Code Explanation:

Function: 
remove_nth_index_char
Purpose:
To return a new string where the character at the specified index n is removed.
Arguments:
input_string (str): The original string from which a character is to be removed.
n (int): The index of the character to remove.

Logic:
Index Validation:
if n < 0 or n >= len(input_string):
Checks if n is outside the valid range of indices for the string.
If n is negative or greater than or equal to the length of the string, it raises a ValueError with the message: "Index n is out of range."

String Manipulation:
input_string[:n]: Slices the string to include all characters before index n.
input_string[n+1:]: Slices the string to include all characters after index n.
input_string[:n] + input_string[n+1:]: Combines the two slices, effectively removing the character at index n.

Return Value:
A new string with the character at the nth index removed.

Main Script:
The if __name__ == "__main__": block allows this script to run only when executed directly, not when imported as a module.

Step-by-Step Execution:
Input:
Prompts the user to enter a non-empty string with input("Enter a non-empty string: ").
Prompts the user to specify the index (n) of the character they want to remove with int(input("Enter the index of the character to remove: ")).

Validation and Function Call:
The script calls remove_nth_index_char with the provided string and index.
If the index is invalid (e.g., out of range), a ValueError is raised, and the except block catches it.

Error Handling:
If an exception occurs (e.g., invalid index), it prints an error message: Error: Index n is out of range.
Output:
If the function executes successfully, it prints the new string with the character removed:
String after removing the nth index character: <new_string>

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (68) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (943) Python Coding Challenge (388) Python Quiz (40) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses