Start learning free

LESSON 272 OF 671 · STRINGS

Strings in Go

Lesson

The Go string is a value that represents a sequence of characters, each identified by one index.

Creation:

To create strings in Go, you use double quotes like this:

greeting := "Hello"

Accessing characters:

To access the bytes in a string, you use the [] operator

The following example returns the first byte of a string with the index zero:

str := "Hello"
ch := str[0]
// ch is the byte 72, the code of 'H'

Getting the length:

We can get the length of a string using the len() function.

For example, to access the last byte of the string, you use the len() - 1 index:

str := "Hello"
ch := str[len(str) - 1]
// ch is the byte 111, the code of 'o'

Concatenating strings via + operator:

To concatenate two or more strings, you use the + operator:

name := "John"
str := "Hello " + name

fmt.Println(str) // prints "Hello John"

If you want to assemble a string piece by piece, you can use the += operator:

greet := "Welcome"
name := "John"
greet += " to AlgoCademy, "
greet += name

fmt.Println(greet) // prints "Welcome to AlgoCademy, John"

String slicing

We often want to get a substring of a string. For this, we use Go's slice notation.

str[startIndex:endIndex] returns the substring starting from startIndex up to (but not including) endIndex:

str := "JavaScript"
substr := str[2:6]

fmt.Println(substr) // prints "vaSc"

The startIndex is a zero-based index at which the slice starts extraction.

The endIndex specifies the position (exclusive) up to which the slice extracts.

If you omit the endIndex, the slice extracts to the end of the string:

str := "JavaScript"
substr := str[4:]

fmt.Println(substr) // prints "Script"

Iterating through the characters:

We can iterate through the characters of a string using a for loop and the range keyword like this:

myStr := "Andy"
for _, c := range myStr {
    fmt.Println(string(c))
}

// This will print the characters 'A', 'n', 'd' and 'y' on different lines

We can also iterate through the elements of a string using indices and a for loop like this:

myStr := "Andy"
for i := 0; i < len(myStr); i++ {
    fmt.Println(string(myStr[i]))
}

// This will print the characters 'A', 'n', 'd' and 'y' on different lines

Both ways take O(n) time, where n is the length of the string we iterate on.


Modifying characters with runes:

In Go, strings are immutable — you cannot change a character with str[i] = .... To edit individual characters, first convert the string into a []rune. A rune is Go's type for a single character (a Unicode code point), so []rune is a modifiable slice of the string's characters:

str := "cat"
chars := []rune(str)   // convert the string into a slice of runes (characters)
chars[0] = 'b'         // now individual characters can be changed
str = string(chars)    // convert the runes back into a string

fmt.Println(str) // prints "bat"


Assignment
Follow the Coding Tutorial and let's play with some strings.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 7
    Ctrl + Enter to check