{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Bala Priya C",
  "title_listed": "Python String Methods for Whitespace Removal",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/balapriya/python-string-methods-for-whitespace-removal-57da",
  "publish_date": "2021-11-25",
  "corpus_stage": "",
  "notes": "",
  "id": "ee312ecf2203",
  "collected_at": "2026-10-02T14:01:25",
  "title": "Python String Methods for Whitespace Removal - DEV Community",
  "body_markdown": "When working with Python strings, there are times when you'll need to remove unwanted whitespaces, tabs, and newline characters.\nYou may have to resize strings, or ensure consistency for further processing - such as processing text collected through forms.\nAnd Python's string methods for whitespace removal return strings with all unwanted whitespaces, and tabs removed.\n📑\nNote\n: I'll use the term whitespaces going forward - whitespaces, tabs, and newlines have all been subsumed in this term 'whitespaces'.\nIn this tutorial, you'll learn how you can remove leading, trailing, and both leading and trailing whitespaces using the built-in string methods,\nlstrip()\n,\nrstrip()\n, and\nstrip()\n, respectively.\nHow to Use\nlstrip()\nto Remove Leading Whitespaces in Python Strings\nLet's start with the\nlstrip()\nmethod.\n<string>.lstrip()\nreturns a copy of the string with all leading whitespaces removed.\nThe\nl\nin\nlstrip()\nstands for left. So this method removes all whitespaces to the left of the first character in the string.\nTherefore it removes leading whitespaces.\n▶ Here's an example.\nConsider the string\n\"    Coding is hard, yet fun!\\t\"\n- it has 4 leading whitespaces, and 1 trailing tab (\n\\t\n).\nNow, call the\nlstrip()\nmethod on this string. And store the returned string in the variable\nl_str\n.\nlong_str\n=\n\"\nCoding is hard, yet fun!\n\\t\n\"\nl_str\n=\nlong_str\n.\nlstrip\n()\nEnter fullscreen mode\nExit fullscreen mode\nLet's print out the lengths of the original string\nlong_str\n, and the returned string\nl_str\n.\nprint\n(\nlen\n(\nlong_str\n))\nprint\n(\nlen\n(\nl_str\n))\n# Output\n29\n25\nEnter fullscreen mode\nExit fullscreen mode\nAs you can see from the lengths, the 4 leading whitespaces have been removed.\nHow to Use\nrstrip()\nto Remove Trailing Whitespaces in Python Strings\nLet's now look at the\nrstrip()\nmethod.\n<string>.rstrip()\nreturns a copy of the string with all trailing whitespaces removed.\nThe\nr\nin\nrstrip()\nstands for right. So this method removes all whitespaces to the right of the last character in the string.\nTherefore it removes trailing whitespaces.\n▶ Let's use the same example string from the previous section. And call the\nrstrip()\nmethod on this string, store the returned string in the variable\nr_str\n.\nlong_str\n=\n\"\nCoding is hard, yet fun!\n\\t\n\"\nr_str\n=\nlong_str\n.\nrstrip\n()\nprint\n(\nlen\n(\nlong_str\n))\nprint\n(\nlen\n(\nr_str\n))\n# Output\n29\n28\nEnter fullscreen mode\nExit fullscreen mode\nAs you can see, the trailing tab\n\\t\nhas been removed. And the returned string's length is one less than the original string, as expected.\nHow to Use\nstrip()\nto Remove Leading and Trailing Whitespaces in Python Strings\nNow that you know how to remove leading and trailing whitespaces using\nlstrip()\nand\nrstrip()\nrespectively, in this section, you'll learn how\nstrip()\nmethod helps remove both leading and trailing whitespaces.\n<string>.strip()\nreturns a copy of the string with all leading and trailing whitespaces removed.\n▶ As with the earlier sections, let's call the\nstrip()\nmethod on the string\nlong_str\n.\nlong_str\n=\n\"\nCoding is hard, yet fun!\n\\t\n\"\ns_str\n=\nlong_str\n.\nstrip\n()\nprint\n(\nlen\n(\nlong_str\n))\nprint\n(\nlen\n(\ns_str\n))\n# Output\n29\n24\nEnter fullscreen mode\nExit fullscreen mode\nFrom the length of the returned string, you can see that both the 4 leading whitespaces, and the trailing\n\\t\nhave been removed.\nConclusion\nHope you found this short tutorial on string methods helpful.\nThe table below summarizes what you've learned:\nMethod\nDescription\nlstrip()\nReturns a copy of the string with all leading whitespaces removed\nrstrip()\nReturns a copy of the string with all trailing whitespaces removed\nstrip()\nReturns a copy of the string with all leading and trailing whitespaces removed\nHappy learning and coding!😄\nCover Image: Photo by\nAntonio Castellano\non\nUnsplash",
  "metadata": {
    "title": "Python String Methods for Whitespace Removal - DEV Community",
    "description": "When working with Python strings, there are times when you'll need to remove unwanted whitespaces,... Tagged with python, tutorial.",
    "keywords": "python, tutorial, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/balapriya/python-string-methods-for-whitespace-removal-57da",
    "og:title": "Python String Methods for Whitespace Removal",
    "og:description": "When working with Python strings, there are times when you'll need to remove unwanted whitespaces,...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpcbq9f5x43y9z3r2xbx0.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Python String Methods for Whitespace Removal",
    "twitter:description": "When working with Python strings, there are times when you'll need to remove unwanted whitespaces,...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpcbq9f5x43y9z3r2xbx0.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/ee312ecf2203/code/body-000.txt",
      "chars": 70,
      "language": "text"
    },
    {
      "file": "attachments/ee312ecf2203/code/body-001.txt",
      "chars": 55,
      "language": "text"
    },
    {
      "file": "attachments/ee312ecf2203/code/body-002.txt",
      "chars": 125,
      "language": "text"
    },
    {
      "file": "attachments/ee312ecf2203/code/body-003.txt",
      "chars": 124,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://unsplash.com/@andoyc?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Antonio Castellano",
      "type": "a"
    },
    {
      "href": "https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Unsplash",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:08Z",
  "source_url": "https://dev.to/balapriya/python-string-methods-for-whitespace-removal-57da",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 3763,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/ee312ecf2203/pages/source.html",
  "local_html": "attachments/ee312ecf2203/pages/article.html",
  "body_base_path": "attachments/ee312ecf2203/pages/",
  "body_markdown_path": "attachments/ee312ecf2203/pages/article.md"
}