Ad Code

Write a Python module that contains a function called remove_special_characters(). This function should take a string as input and return a new string with all special characters removed. Only lowercase and uppercase letters, numbers, and spaces should be allowed in the returned string

Write a Python module that contains a function called remove_special_characters(). This function should take a string as input and return a new string with all special characters removed. Only lowercase and uppercase letters, numbers, and spaces should be allowed in the returned string.

Codes:

import re


def remove_special_characters(input_string):

    # Define regex pattern to match special characters

    pattern = r'[^a-zA-Z0-9\s]'


    # Use regex to substitute special characters with empty string

    cleaned_string = re.sub(pattern, '', input_string)


    return cleaned_string


# Example usage

if __name__ == "__main__":

    input_string = "Hello! How are you? 1234"

    cleaned_string = remove_special_characters(input_string)

    print("Input String:", input_string)

    print("Cleaned String:", cleaned_string)


Reactions

Post a Comment

0 Comments