No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python rstrip lstrip

by Khaleel O.
September 6, 2021
in Python
Reading Time: 3 mins read
A A
Python rstrip lstrip
Python rstrip and lstrip

Python rstrip and lstrip returns a copy of the string with right and left trailing characters removed, respectively.

s = "     FirstName     "
print(f"{s.rstrip()} LastName")

#Output
#     FirstName LastName

By default rstrip will remove the trailing whitespaces to the right of the string, as seen above. We also use an f-string which you can found more about HERE.

There is an optional argument accepted by rstrip that allows us to specify an exact character to be excluded instead. In this case it will remove the specified character:

s = "-----FirstName------"
print(f"{s.rstrip('-')} LastName")

#output
#-----FirstName LastName

Note that rstrip only works on the right hand side, the specified character or whitespace will remain on the left side of the string.

Similarly, by default lstrip will remove the trailing whitespaces to the left of the string. Method lstrip also allows us to specify the optional argument:

s = "     FirstName     "
print(f"{s.lstrip()} LastName")

#output
#FirstName      LastName

s = "______FirstName______"
print(f"{s.lstrip('_')} LastName")

#output
#FirstName______ LastName

Note that lstrip only works on the left hand side, the specified character or whitespace will remain on the right side of the string.

Finally we can use both lstrip and rstrip at the same time. This will strip or remove the whitespace or specified character as follows:

s = "          FirstName       "
print(f"{s.lstrip().rstrip()} LastName")

#output
#FirstName LastName

s = "______FirstName______"
print(f"{s.lstrip('_').rstrip('_')} LastName")

#output
#FirstName LastName

We can actually accomplish the same thing as above with strip. The strip method trims both the left and right side of the specified string. It works with both whitespaces and a specified character like lstrip and rstrip:

s = "______FirstName______"
print(f"{s.strip('_')} LastName")

#output
#FirstName LastName

s = "      FirstName      "
print(f"{s.strip()} LastName")

#output
#FirstName LastName

Finally, we can strip multiple characters if we wish:

s = "<<<<<<<<       FirstName         >>>>>>"
print(f"{s.strip('<> ')} LastName")

#output
#FirstName LastName

In the above example we stripped three characters from the right and left: <, > and ‘ ‘ (whitespace).

Find the full source code HERE. Thanks for reading! 👌👌👌

Check out our Python String Formatting tutorial HERE.

Previous Post

Python writelines With Examples

Next Post

PYTHON RSTRIP

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy