How to Remove Characters From a String in Python

By Kevin Walker

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 1

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

Step 2

Type **python** to open the Python interpreter.

Step 3

Define a string by typing:s = "Hello World."

Step 4

Type the following to strip the letters "o" and "l" from the string:s = s.strip(['o','l'])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 5

Type the following to remove an entire word in the string:s = s.replace("World", "")

Tips & Warnings

  • To remove individual characters anywhere they occur in a string, use "strip." If you wish to remove entire sequences of characters, such as words, then use "replace."

References & Resources