DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


Python 3 useful tip-strip()-A useful built-in type.

  • 2 minutes read
  • 122 Views
Python 3 useful tip-strip()-A useful built-in type.

    Table of Contents

Have you ever stumbled across a set of data that contains an extra underscore, character, or word as prefix or suffix, which you want to get rid of?

Python is known for having a resourceful standard library with lots of built-in types that can do a set of tasks in a jiffy. One such built-in type is str.strip([chars]). This built-in type in theory omits the set of characters given as an argument from the beginning and end of string. If no character argument is given, it removes the whitespace from the string. One of the example of the usage is as under:-

example_string = '    anexamplestring    '
example_string.strip()

"anexamplestring"

However, if we add certain characters in ‘char’ arguments, those characters will be removed from the beginning and end of the string until a character not given in the argument is reached on both side of the string. Example as under:-

example_string = '123_ab_1cde_3'
example_string.strip('123_')

"ab_1cde"

It must be noticed that the .strip() function stop deleting the characters from either side as soon as it reaches a character which is not passed in the argument. And, it will not remove the characters thereafter even if they are the one passed in the argument as in example above, once the strip() reaches the character ‘a’ moving from left to right, it stopped removing character and didn’t effect ‘_’ and ‘1’ thereafter. Similarly, strip() moving from right to left encounters character ‘e’ and stop functioning thereafter.


Related Posts

Set Passwords and Secret Keys in Environment Variables (Mac/Linux/Windows) - Python Quicktip
By Udit Vashisht

Hide Passwords and Secret Keys in Environment Variables

If you are into python , there is a fair chance that you would have contributed to open-source or had your code snippets/projects on Github or BitBucket.Some time your code involves some important credentials like passwords or secret keys ...

Read More
Python Logging Module - A Primer
By Udit Vashisht

Logging in python

Logging is the module in the standard library of python. Logging is an important tool for a programmer or developer, as it gives more insights about the code and any errors in it. In this tutorial, we will learn all about the python logging module. ...

Read More
Difference between "==" and "is" in Python- Quick Tip
By Udit Vashisht

What is the difference between “==” and “is” variable in Python?

If you are new to python you must have seen programmers using “==” and “is”, sometimes even interchangeably. Prima-facie, they look similar and many times you will use any of them. But there is a big difference between them. ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python