Loading video player…

Strip Characters From a Python String (Overview)

By default, Python’s .strip() method removes whitespace characters from both ends of a string. To remove different characters, you can pass a string as an argument that specifies a set of characters to remove. The .strip() method is useful for tasks like cleaning user input, standardizing filenames, and preparing data for storage.

By the end of this video course, you’ll understand that:

  • The .strip() method removes leading and trailing whitespace but doesn’t remove whitespace from the middle of a string.
  • You can use .strip() to remove specified characters from both ends of the string by providing these characters as an argument.
  • With the related methods .lstrip() and .rstrip(), you can remove characters from one side of the string only.
  • All three methods, .strip(), .lstrip(), and .rstrip(), remove character sets, not sequences.
  • You can use .removeprefix() and .removesuffix() to strip character sequences from the start or end of a string.

Resource mentioned in this lesson: Strings and Character Data in Python

Download

Course Slides (.pdf)

6.6 MB
Download

Sample Code (.zip)

2.0 KB

00:00 Hey there. Welcome to Strip Characters from a Python String. My name’s Joseph, and I’ll be your explainer of things for this video course. Pretty much every Python developer has to handle and process strings, and because strings can be used to represent virtually any kind of textual data, they’re incredibly versatile.

00:17 But with that versatility comes the potential for awkward formatting, unnecessary white space, or just plain, messy text. This is part of the reason why Python includes literally dozens of string methods, many of which can be used to clean up a string or have it fit a desired format.

00:34 This course is all about the specific collection of methods you can use to remove characters or sequences of characters from both ends of a string. They’re super useful for handling any kind of user inputs, standardizing data for inputting into a database, cleaning up text collected via web scraping, and so on.

00:52 In this course, you’ll learn how to remove white space or specific characters from strings with .strip(). How to decide when to use .strip(), .lstrip() or .rstrip().

01:02 How to use .removeprefix() and .removesuffix() to remove sequences of characters, how to apply string methods to real world use cases, and how to avoid common mistakes made when using .strip().

01:15 To get the most out of this course, you only really need to be familiar with some Python basics, strings and characters, built-in Python data types, and functions and methods.

01:25 If you feel like you need a refresher on strings, you can check out Strings and Character Data in Python. It’s quite a long tutorial, but the basics are covered by the first few sections.

01:36 Alright, join me in the next lesson where we’ll explore the .strip() method.

Become a Member to join the conversation.