ProgramGuru

String Length and Indexing

Start with String Length and Indexing

The built-in len function returns a string’s byte length, not its count of Unicode characters. Indexing a string reads one byte.

What you’ll learn

  • Explain the main purpose of string length and indexing.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

ASCII letters occupy one byte each in UTF-8, so byte length and letter count coincide for simple English text. Many other characters use more than one byte. The expression text[i] returns the byte at offset i, not a full Unicode code point.

Use utf8.RuneCountInString when you need the count of decoded Unicode code points. Even a rune count is not necessarily the same as the number of user-perceived characters, because a visual character can contain multiple code points.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for string lengthA rune can occupy more than one byte in UTF-8.c a f (ASCII)Step 1é (2 UTF-8 bytes)Step 2Total: 5 bytesStep 3Total: 4 runesStep 4A rune can occupy more than one byte in UTF-8.
A rune can occupy more than one byte in UTF-8.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	ascii := "Go"
	accented := "café"
	fmt.Println("Go bytes:", len(ascii))
	fmt.Println("café bytes:", len(accented))
	fmt.Println("café runes:", utf8.RuneCountInString(accented))
	fmt.Printf("First byte: %c\n", ascii[0])
}

Output

Go bytes: 2
café bytes: 5
café runes: 4
First byte: G

Trace what the program does

Go uses two ASCII bytes. The word café has three ASCII characters plus é encoded as two UTF-8 bytes, so len reports five bytes but RuneCountInString reports four code points. Index 0 of Go is the byte for G.

Avoid common mistakes

  • len(text) does not count visible characters for general Unicode input.
  • Indexing into the middle of a multibyte encoding returns one byte, not a meaningful standalone rune.

Try it yourself

Compare len and utf8.RuneCountInString for the string "A😀B". Predict both counts before running.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What does len("café") count?
Choose one answer. You can try again.

Answer and explanation

Bytes

Correct. Go uses two ASCII bytes.

What you should remember

You have practiced String Length and Indexing in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: String Comparison →