In: Computer Science
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also.
Hint: You can invoke the isalnum() function on a character, and it will return True if the string is alphanumeric, False if not. ‘5’.isalnum() -> True. You can invoke the isspace() function on a character to check if the character is a space or not.
Here is the answer for your question in Python Programming Language.
Kindly upvote if you find the answer helpful.
##########################################################
CODE :
#read user input #Using for loop, loops through each character of
string newString = "" print("Using Method 1 : WHILE LOOP, the new string is \"{0}\"".format(newString)) |
##################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
####################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)