Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk1 min

Get the First N Characters of a String in Python

Get a string prefix in Python with s[:n]. The stop is exclusive, oversized slices are clipped, and Python counts Unicode code points rather than bytes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a slice: s[:n] returns the first n positions of a string. For example, "Python"[:3] evaluates to "Pyt". The start is omitted, so it defaults to zero; the stop position is not included.

Use a slice to get the prefix

Put the string before the brackets and the number of characters you want after the colon:

text = "Python"
prefix = text[:3]
print(prefix)  # Pyt

In s[start:stop], Python includes the start position and stops before the stop position. Leaving out start makes the slice begin at position zero. The official Python tutorial describes these defaults; the sequence operations documentation gives the corresponding slice behavior.

What happens at the boundaries

  • n is zero: s[:0] returns an empty string.
  • n exceeds the string length: the slice returns the whole string; it does not raise an index error.
  • n is negative: Python applies negative slice-bound rules. For example, s[:-1] returns all but the final code point, not a negative number of characters.

This makes a slice useful when the string may be shorter than the requested length: s[42] raises IndexError if that position is missing, while s[:42] safely stops at the string’s end. Slicing produces a new string value and does not alter the original; Python strings are immutable.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What Python counts as a character

Python strings are sequences of Unicode code points, not encoded bytes. So s[:n] counts code-point positions. A visible character can consist of multiple code points—for example, a letter followed by a separate combining accent—so a slice can split a displayed character. Python slicing does not promise boundaries that match user-perceived grapheme clusters. These distinctions follow from Python’s Unicode documentation.

If you need a byte limit, choose an encoding and work with the encoded bytes deliberately; a string slice is not a byte limit. If you need to preserve visible grapheme clusters, code-point slicing alone may not meet that requirement.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.