{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Bala Priya C",
  "title_listed": "String Slicing in Python, Explained",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/balapriya/string-slicing-in-python-explained-5edg",
  "publish_date": "2022-01-03",
  "corpus_stage": "",
  "notes": "",
  "id": "4edeac79d584",
  "collected_at": "2026-10-02T14:01:19",
  "title": "String Slicing in Python, Explained - DEV Community",
  "body_markdown": "String slicing lets you slice into Python strings, and work with their\nslices\n, or\nsubstrings\n—instead of the whole string.\nAs strings in Python are\nimmutable\n, you cannot change them in place. In this tutorial, you'll learn to slice Python strings and work with substrings.\nPython String Slicing Syntax\n<\nstr\n>\n[\nstart\n:\nstop\n:\nstep\n]\nEnter fullscreen mode\nExit fullscreen mode\nThe above line of code:\nReturns a slice of the string\n<str>\n—starting at index\nstart\n, extending up to\nstop-1\nin steps of\nstep\n.\nThe\nstart\nindex is\noptional\n: the slice starts from the beginning of the string by\ndefault\n.\nThe\nstop\nindex is also\noptional\n: the slice extends up to the end of the string by\ndefault\n.\nThe\nstep\nvalue is optional too. The default value of step is\n1\nand includes all characters in the string.\nPython String Slicing Example\nmy_str\n=\n\"\nPython3\n\"\nEnter fullscreen mode\nExit fullscreen mode\n▶ Let's use\nenumerate()\nand examine the characters at each index in the string.\nfor\nidx\n,\nchar\nin\nenumerate\n(\nmy_str\n):\nprint\n(\nf\n\"\nAt index\n{\nidx\n}\n: letter\n{\nchar\n}\n\"\n)\n# Output\nAt\nindex\n0\n:\nletter\nP\nAt\nindex\n1\n:\nletter\ny\nAt\nindex\n2\n:\nletter\nt\nAt\nindex\n3\n:\nletter\nh\nAt\nindex\n4\n:\nletter\no\nAt\nindex\n5\n:\nletter\nn\nAt\nindex\n6\n:\nletter\n3\nEnter fullscreen mode\nExit fullscreen mode\nIn the above code, you've used the\nenumerate()\nfunction in conjunction with the\nfor\nloop. This lets you loop through iterables, and access items along with their indices simultaneously—without having to use the\nrange()\nfunction to get the indices.\n▶ Let's now use string slicing.\n# With `start` and `stop` indices\nprint\n(\nmy_str\n[\n1\n:\n6\n])\n# Output: ython\n# Without `stop` index\nprint\n(\nmy_str\n[\n1\n:])\n# Output: ython3\n# Without `start` index\nprint\n(\nmy_str\n[:\n5\n])\n# Output: Pytho\n# With `step = 2`, slice includes every second character\nprint\n(\nmy_str\n[::\n2\n])\n# Output: Pto3\n# Without `start`, `stop` and `step`: slice is entire string\nprint\n(\nmy_str\n[::])\n# Output: Python3\nEnter fullscreen mode\nExit fullscreen mode\nPython String Slicing with Negative Step\nWhen you set\nstep\nto a negative value, you can get slices starting from the end of the string—reverse substrings.\nIf\nstep = -1\nyou get a slice starting from the end of the string, and including every character.\nThis can be super handy when you'd like to\nreverse\na string, like this:\nprint\n(\nmy_str\n[::\n-\n1\n])\n# Output: 3nohtyP\nEnter fullscreen mode\nExit fullscreen mode\nConclusion\nTo sum up,\n<str>[start:stop:step]\nis the syntax to obtain string slices or substrings in Python.\nNow that you've learned how to slice strings, it's time to put your skills to practice, maybe? Happy learning and coding!\nCover image: Photo by Tamanna Rumee on Unsplash",
  "metadata": {
    "title": "String Slicing in Python, Explained - DEV Community",
    "description": "String slicing lets you slice into Python strings, and work with their slices, or substrings—instead... Tagged with python, programming, codenewbie, tutorial.",
    "keywords": "python, programming, codenewbie, tutorial, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/balapriya/string-slicing-in-python-explained-5edg",
    "og:title": "String Slicing in Python, Explained",
    "og:description": "String slicing lets you slice into Python strings, and work with their slices, or substrings—instead...",
    "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%2Fg2jdtakg18jegqdght4j.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "String Slicing in Python, Explained",
    "twitter:description": "String slicing lets you slice into Python strings, and work with their slices, or substrings—instead...",
    "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%2Fg2jdtakg18jegqdght4j.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/4edeac79d584/code/body-000.txt",
      "chars": 23,
      "language": "text"
    },
    {
      "file": "attachments/4edeac79d584/code/body-001.txt",
      "chars": 19,
      "language": "text"
    },
    {
      "file": "attachments/4edeac79d584/code/body-002.txt",
      "chars": 234,
      "language": "text"
    },
    {
      "file": "attachments/4edeac79d584/code/body-003.txt",
      "chars": 377,
      "language": "text"
    },
    {
      "file": "attachments/4edeac79d584/code/body-004.txt",
      "chars": 39,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:07Z",
  "source_url": "https://dev.to/balapriya/string-slicing-in-python-explained-5edg",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 2680,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/4edeac79d584/pages/source.html",
  "local_html": "attachments/4edeac79d584/pages/article.html",
  "body_base_path": "attachments/4edeac79d584/pages/",
  "body_markdown_path": "attachments/4edeac79d584/pages/article.md"
}