The problem statement is to implement the function isWhiteLine(), which takes a string…
- User
The problem statement is to implement the function isWhiteLine(), which takes a string and returns TRUE if the string contains only white space & tab characters.
The input specification is that input1 is a string, which is a string where the specified condition needs to be checked.
The output specification is that the function should return a boolean True if the given string has only whitespace and tab characters, else it should return False.
An example is provided: input1: "This is a test string" output: False
input1: " \t" output: True
The explanation states that if the given string contains any other character other than \r, \t, \n or space, the function should return False.
- Assistant
Sure, let's implement the function
isWhiteLine()to validate if a given string contains only whitespace, tab, newline, or carriage return characters. We'll systematically check each character in the string to ensure they fall within these specified whitespace categories.Here is the code for the function:
def isWhiteLine(input1): # Loop through each character in the string for char in input1: # Check if the character is not in the list of valid whitespace characters if char not in [' ', '\t', '\r', '\n']: return False # If all characters passed the check, return True return True # Test cases print(isWhiteLine("This is a test string")) # Expected output: False print(isWhiteLine(" \t")) # Expected output: True print(isWhiteLine(" ")) # Expected output: True print(isWhiteLine("")) # Expected output: True (empty string, no invalid characters) print(isWhiteLine("\n\r\t ")) # Expected output: True print(isWhiteLine(" \t some text \n")) # Expected output: False