Saturday, 28 December 2024

Day 64 : Python Program to Remove Odd Indexed Characters in a string


 def remove_odd_indexed_chars(input_string):

    """

    Remove characters at odd indices from the input string.

      Args:

        input_string (str): The string to process.

        Returns:

        str: A new string with characters at odd indices removed.

    """

    return input_string[::2]

if __name__ == "__main__":

    input_string = input("Enter a string: ")

    result = remove_odd_indexed_chars(input_string)

    print("String after removing odd indexed characters:", result)

#source code --> clcoding.com 

Code Explanation:

1. Function Definition
def remove_odd_indexed_chars(input_string):
def: Used to define a function.
remove_odd_indexed_chars: The name of the function, which describes its purpose (removing odd-indexed characters).
input_string: A parameter that represents the input string the user provides.

2. Docstring
    """
    Remove characters at odd indices from the input string.

    Args:
        input_string (str): The string to process.

    Returns:
        str: A new string with characters at odd indices removed.
    """
A docstring provides an explanation of the function:
Purpose: The function removes characters at odd indices.
Args: It expects one argument:
input_string: The string to be processed.
Returns: It outputs a new string with the odd-indexed characters removed.

3. Function Logic
    return input_string[::2]
input_string[::2]: This is a slicing operation. Here's how it works:
Start (start): By default, starts at index 0 (the first character).
Stop (stop): By default, goes to the end of the string.
Step (step): Specifies the interval; 2 means take every second character.

Effect:
Only characters at even indices (0, 2, 4, ...) are retained.
Characters at odd indices (1, 3, 5, ...) are skipped.
The resulting string is returned to the caller.

4. Main Execution Block
if __name__ == "__main__":
if __name__ == "__main__"::
Ensures the code inside this block runs only when the script is executed directly (not imported as a module in another script).

5. Input Prompt
    input_string = input("Enter a string: ")
input("Enter a string: "):
Prompts the user to enter a string.
The entered string is stored in the variable input_string.

6. Function Call
    result = remove_odd_indexed_chars(input_string)
remove_odd_indexed_chars(input_string):
Calls the function remove_odd_indexed_chars with the user-provided string (input_string) as an argument.
The function processes the string and returns a new string with odd-indexed characters removed.
result:
Stores the returned value (the modified string).

7. Output
    print("String after removing odd indexed characters:", result)
print:
Outputs the result to the user.
Displays the modified string with odd-indexed characters removed.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (65) 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 (3) Pandas (4) PHP (20) Projects (29) Python (943) Python Coding Challenge (382) Python Quiz (38) 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