How to Remove Characters From a String in Python

One of the useful features of the Python programming language is the numerous methods it offers for text manipulation. You can use two methods to remove characters from a string of text; the method you choose depends on whether you want to remove from the string individual letters or specific letter sequences.

Step

Open the command prompt. In Windows, press the Windows-R keys and then type cmd in the Run field.

Step

Type python to open the Python interpreter.

Step

Define a string by typing:

Step

s = "Hello World."

Step

Type the following to strip the letters "o" and "l" from the string:

Step

s = s.strip(['o','l'])

Step

You must reassign the string to its own variable name. Strings in Python cannot be altered after they are defined. Instead, "strip" -- and other string methods -- produce a new string that you must assign to the old variable name.

Step

Type the following to remove an entire word in the string:

Step

s = s.replace("World", "")