Computer Science, asked by sandhiyavisu16, 4 hours ago

python program to remove punctuation marks from a string using function​

Answers

Answered by Equestriadash
5

\tt s = in put("Enter\ a\ string: ")\\for\ i\ in\ s:\\{\ \ \ \ \ }if\ i\ in\ "!:;.-,'"\ or\ i\ in\ '"':\\{\ \ \ \ \ }{\ \ \ \ \ }ns\ =\ s.replace(i,\ "")\\print(ns)

Once the string is input, we start a loop to iterate/traverse through the characters in the string. During the process of iterating, we use a conditional statement to check if the character is present in the string of punctuation marks or not. If it is present, we replace it with an empty string using an in-built function called replace().

The syntax of the replace() function is as follows:

<string_name>.replace(old string, new string)

We store the new string into a new variable since the replace function does not permanently change the character and only changes it for display purposes.

For example,

&gt;&gt;&gt;\ \tt n\ =\ "Vanessa"\\&gt;&gt;&gt;\ n.replace('V', 'J')\\'Janessa'\\&gt;&gt;&gt; print(n)\\Vanessa

As observed above, the replace() function temporarily changes the string. To make it permanent, you'd have to perform the conversion into a new string.

&gt;&gt;&gt;\ \tt n\ =\ "Vanessa"\\&gt;&gt;&gt;\ ns\ =\ n.replace('V', 'J')\\&gt;&gt;&gt; print(ns)\\Janessa

Similar questions