Write a program to remove all digit in given string.Below given is the program to remove all punctuation's in a string and print the result.import stringpunctuations = string.punctuationresult = " "str = "List - []\n tuple - ()\n Dictionary - {}\n Comment - #\n Multiply - *\n not - !\n and - &\n or - |\n format specifier - %\n String - " " $ @ ; : ' / + = " for i in str: if i not in punctuations: result = result + iprint("String after removing all Punctuation's:", result)Here,We are importing string module to know the list of punctuation's using string.punctuation. Then we compare punctuations in the given string with the string.punctuation module and remove them from the input string and print the resultant string. str variable contains input string. for clause to iterate over the list of characters in the input string. if condition to check if a punctuation of the input string exists in punctuation variable or not.Output:Set of punctuations in string.punctuation is: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~String after removing all Punctuation's is: List tuple Dictionary Comment Multiply not and or format specifier String Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming
Question
Write a program to remove all digit in given string.Below given is the program to remove all punctuation's in a string and print the result.import stringpunctuations = string.punctuationresult = " "str = "List - []\n tuple - ()\n Dictionary - {}\n Comment - #\n Multiply - \n not - !\n and - &\n or - |\n format specifier - %\n String - " " @ ; : ' / + = " for i in str: if i not in punctuations: result = result + iprint("String after removing all Punctuation's:", result)Here,We are importing string module to know the list of punctuation's using string.punctuation. Then we compare punctuations in the given string with the string.punctuation module and remove them from the input string and print the resultant string. str variable contains input string. for clause to iterate over the list of characters in the input string. if condition to check if a punctuation of the input string exists in punctuation variable or not.Output:Set of punctuations in string.punctuation is: !"#%&'()+,-./:;<=>?@[]^_`{|}~String after removing all Punctuation's is: List tuple Dictionary Comment Multiply not and or format specifier String Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming
Solution
Sure, here is a Python program that removes all digits from a given string:
# Function to remove all digits from the string
def remove_digits(str):
result = ''.join([i for i in str if not i.isdigit()])
return result
# Test Case 1
str1 = "Hello123World"
print("str: ", str1)
print("String after removing all digits: ", remove_digits(str1))
# Test Case 2
str2 = "1234567890"
print("str: ", str2)
print("String after removing all digits: ", remove_digits(str2))
# Test Case 3
str3 = "PythonProgramming"
print("str: ", str3)
print("String after removing all digits: ", remove_digits(str3))
In this program, we define a function remove_digits that takes a string as input and returns a new string that contains only the characters from the input string that are not digits. This is done using a list comprehension that iterates over each character in the input string and includes it in the new string only if it is not a digit. The isdigit method of a string returns True if the string is a digit and False otherwise. The join method of a string is used to concatenate all the characters in the list into a new string.
Similar Questions
Now build a program to take string (combination of characters and digits) as input from the user and remove all the digits from the given string.Print the result as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Hello123WorldString·after·removing·all·digits:·HelloWorldTest Case 2:Expected Output:str:·1234567890String·after·removing·all·digits:·Test Case 3:Expected Output:str:·PythonProgrammingString·after·removing·all·digits:·PythonProgramming
You are given a string s.Your task is to remove all digits by doing this operation repeatedly:Delete the first digit and the closest non-digit character to its left.Return the resulting string after removing all digits. Example 1:Input: s = "abc"Output: "abc"Explanation:There is no digit in the string.Example 2:Input: s = "cb34"Output: ""Explanation:First, we apply the operation on s[2], and s becomes "c4".Then we apply the operation on s[1], and s becomes "". Constraints:1 <= s.length <= 100s consists only of lowercase English letters and digits.The input is generated such that it is possible to delete all digits.
What will be the output of the following code? str1="an apple a day keeps doctor away"lis=str1.split(" ")for word in lis: if word.startswith("d") : lis.remove(word)print(lis)
Write a Python program to remove the characters which have odd index values of a given string.Note: Index starts from 0Input format :Input consists of a string.Output format :Output prints the string after removing the characters which have odd index values.Sample test cases :Input 1 :placementOutput 1 :paeetInput 2 :examlyOutput 2 :eal
Python program that accepts a string and calculate the number of digits and letters and space
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.