All TalkersCode Topics

Follow TalkersCode On Social Media

Python Strip Multiple Characters

Last Updated : Jan 1, 2023

Python Strip Multiple Characters

In this tutorial we will show you the solution of python strip multiple characters, we will demonstrate to you in this tutorial how to strip (or delete) multiple characters from a Python string. Strings can be declared with single (‘ ‘), double (“ “) or triple quotes (“”” “””).

You will learn the strip() method of Python strings. Let’s begin.

Step By Step Guide On Python Strip Multiple Characters :-

st1 ='.+/mReggae'
st1_cln = st1.strip('.+/m')
print(st1_cln)
st2 = '?@Funkpop#$'
st2_cln = st2.strip('$#@?')
print(st2_cln)
  1. We will use VS Code in this example.
  2. First, we declare a string variable st1. Please note that Python variables are dynamic in nature, i.e we do not need to declare its variable type manually. As soon as data is assigned to the variable, Python evaluates it’s data type.
  3. Clearly you can see that we want the string value ‘Reggae’ but it has some impurities in the form of special characters like ., +, / and also an alphabet m. We will remove it in a Pythonic way using the string strip() method!
  4. The strip() method removes leading and trailing characters around a string. By default, this method removes spaces. However we can mention what we want to remove.
  5. So by the definition, if our raw string is ‘…Python…’ and we apply strip() method mentioning ‘…’ to remove, we will get ‘Python’ finally. You can see both the leading … and trailing … have been removed, living up to the definition.
  6. We pass the argument ‘.+/m’ to strip() method and apply it on st1 using dot operator. Then we assign this modified value back to st1 and print it.
  7. You can see the desired result in the terminal. Note that we passed the argument in the exact order it’s in the string.
  8. Similarly, for the next example we declare another string variable st2 and assign to it a raw value. Clearly, we want our result to be ‘Funkpop’.
  9. In the exact same way, we pass the characters we want to remove from our raw string using strip() method and apply it on st2 using dot operator. Thereafter we print the result. Evidently, we get the desired output ‘Funkpop’.
  10. Did you notice that the argument characters ‘$#@?’ were not in the same order as in the raw string’s ‘?@#$’? This shows that order doesn’t matter!
  11. The only thing we have to keep in mind is that if we want to remove some characters from the middle of some string, for e.g ‘^’ from ‘Sk^y’, strip() method wont come in handy here and we have to employ other methods. Nevertheless, strip() method is highly useful and saves a lot of our time!
  12. You can use any characters you want to remove with the only requirement that they have to be either in leading, trailing or leading & trailing positions.

Conclusion :-

I hope this tutorial on python strip multiple characters helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials