By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A string holds text: letters, digits, spaces and punctuation. You write one between double quotes, like "coffee" or "Penny". The quotes are not part of the value; they show Go where the text starts and where it ends.
Strings are one of the types you can join with +. Joining is called concatenation, and it builds a new string from the pieces in order.
package main
import "fmt"
func main() {
first := "Penny"
last := "the expense tracker"
title := first + " - " + last
fmt.Println(title)
greeting := "Hello, "
greeting += "Penny"
fmt.Println(greeting)
}+= is shorthand for storing the result back into the same variable. It works for numbers too: total += 5 adds five to total, exactly as total = total + 5 would.
The built-in function len returns the number of bytes in a string. For plain English text one letter is one byte, so len feels like a character count. For accented letters, other scripts and emoji it is not: those characters take two, three or four bytes in the UTF-8 encoding that Go uses.
A rune is one character, stored as a number. Range over a string, or call utf8.RuneCountInString, when you want characters. Use len when you really do want bytes. The diagram shows the two views of the same text.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
word := "Penny"
fmt.Println("bytes:", len(word))
fmt.Println("runes:", utf8.RuneCountInString(word))
word = "café"
fmt.Println("bytes:", len(word))
fmt.Println("runes:", utf8.RuneCountInString(word))
}Read the last two lines together: 5 bytes, 4 runes. Each byte still belongs to the text, and Go counts them honestly. When the question is how much storage a string takes, len is right. When the question is how many characters a reader sees, count runes.
fmt.Println prints values separated by spaces. When you need a sentence with values inside it, or columns that line up, use fmt.Printf. The first argument is a pattern. The percent signs are slots, and the arguments that follow fill those slots in order.
| Verb | Prints | Example output |
|---|---|---|
%s | A string | coffee |
%d | An integer | 450 |
%.2f | A float with two decimal places | 4.50 |
%q | A string inside double quotes | "coffee" |
%t | A bool | true |
%v | Any value in its default form | 1250 |
%% | A literal percent sign | % |
package main
import "fmt"
func main() {
item := "coffee"
cents := 450
rate := 0.0837
fmt.Printf("%s costs %d cents\n", item, cents)
fmt.Printf("%.2f dollars\n", float64(cents)/100)
fmt.Printf("%q\n", item)
fmt.Printf("rate: %.2f%%\n", rate*100)
}The pattern ends with the two characters \n, which mean a newline. Printf does not add one for you, so a missing newline is a common reason for two lines running together. The %% in the last pattern prints one literal percent sign after the number.
fmt.Sprintf works like Printf but returns the finished string instead of printing it. Use it when you need to store the text, join it with something else, or return it from a function. That is the pattern Penny uses for receipts: build the line, then print it.
The standard library's strings package collects the text operations you would otherwise write by hand. TrimSpace removes spaces at both ends, ToUpper changes case, Contains answers a yes or no question about a substring, and ReplaceAll swaps every occurrence of one piece of text for another.
package main
import (
"fmt"
"strings"
)
func main() {
raw := " Coffee "
clean := strings.TrimSpace(raw)
receipt := fmt.Sprintf("%s costs %d cents", strings.ToUpper(clean), 450)
fmt.Println(receipt)
fmt.Println(strings.Contains(clean, "off"))
fmt.Println(strings.ReplaceAll(clean, "Coffee", "Tea"))
fmt.Println(len(clean))
}Rewrite the receipt example with your own item, price and rate. Add a line that prints $12.50 for 1250 cents and another that prints $4.50 for 450 cents, both built with fmt.Sprintf. Then take the text " tea ", trim it, and print it between two pipe characters like |tea|: if you can see the pipes touching the word, the spaces are gone.
Key takeaways
Strings are joined with +; += stores the joined result back in the same variable.
len counts bytes and runes count characters; they differ once text is not plain ASCII.
fmt.Printf fills percent slots in a pattern, and fmt.Sprintf builds the string instead of printing it.
strings.TrimSpace, ToUpper, Contains and ReplaceAll cover most beginner text work.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.