October 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 NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Get the Length of a String in Python

Python’s built-in len() returns the number of Unicode code points in a string. Use encoding first when you need a byte count.

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.

Use Python’s built-in len() function: len(my_string). For a Python str, the result is the number of Unicode code points in the string—not its encoded byte count or necessarily the number of symbols a person sees.

Get a string’s length with len()

Pass the string to the built-in function. It returns an integer.

text = "Python"
print(len(text))  # 6

You can also pass a string literal directly:

len("Python")  # 6

This is Python’s documented string-length operation; use len(my_string), not my_string.len(). The Python 3.14.8 built-in types documentation defines len(s) as the length operation.

What does the length count?

For a Python str, len() counts Unicode code points. Python describes strings as immutable sequences of Unicode code points in its built-in types documentation; the Python 3.14.7 Unicode objects and codecs documentation also describes Unicode length in code points.

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

A code point is not always one user-perceived character. An accented letter may be stored as one precomposed code point or as a base letter plus a combining mark. Likewise, an emoji displayed as one symbol may be made from multiple code points. So len(text) answers how many code points are in the Python string, not necessarily how many visible symbols a person would count.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Count encoded bytes instead

If you need the number of bytes in a particular encoding, encode the string first and measure the resulting bytes object. For UTF-8:

text = "Python"
byte_count = len(text.encode("utf-8"))

This counts the bytes in the UTF-8 representation, which is a different measurement from the code-point count of the original str. For user-perceived characters, neither measurement necessarily gives the desired count; that requires higher-level text segmentation into grapheme clusters.

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.

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
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.