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 Count Characters in a String in Python

Python’s len() returns the number of Unicode code points in a string. For visible grapheme clusters or UTF-8 bytes, use a different counting method.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Python’s built-in len() function to get the length of a string: len(text). For a Python str, that counts Unicode code points—not always the number of characters a person perceives on screen. If you need UTF-8 bytes instead, encode the string first.

Count a Python string’s length with len()

For ordinary string length, pass the string to len():

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

The official Python tutorial describes len() as the built-in function that returns a string’s length. Python strings are immutable sequences of Unicode code points, and Python does not have a separate character type; indexing a string produces another string of length one.

What does len() count?

len(text) counts code points. That can differ from the number of symbols a reader sees as individual characters: a letter with a combining accent may consist of a base letter and a separate accent code point, and some emoji are sequences of multiple code points.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

If a specification says “character count,” check which unit it requires:

  • Code points: use len(text), the usual Python string length.
  • User-perceived characters: count grapheme clusters using Unicode-aware segmentation.
  • Encoded bytes: encode the string in the required format, then measure the resulting bytes.

Python 3.15.0rc3 documentation describes unicodedata.iter_graphemes() for iterating extended grapheme clusters under Unicode Standard Annex #29. Because that documentation is for a release candidate, check that the API exists in your target Python interpreter before relying on it.

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

Count UTF-8 bytes instead

For a byte limit or protocol requirement, measure the encoded representation rather than the string’s code-point length:

text = "café"
byte_length = len(text.encode("utf-8"))
print(byte_length)

str.encode() returns a bytes object. The Python codecs documentation explains the distinction between strings and encoded byte representations. A UTF-8 byte count can differ from len(text), particularly for non-ASCII text.

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

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
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.