World 1-1 The Fundamentals : GDScript intro

Part 14: Strings in GDScript

Learn what strings are, how to use them in Godot, and how to combine them, format them, and manipulate text the beginner-friendly way.

🧵 Strings in GDScript: Let’s Talk Text!

So far we’ve printed numbers, worked with floats, and learned about types. But what if we want to show names, dialogue, messages, or any kind of text? That’s where strings come in.

A string is just a bit of text—letters, numbers, or symbols—wrapped in quotation marks. For example:

gdscript
1
2
3

var greeting = "Hello, world!"

Godot knows this is a string because of the " quotes.


🧪 String Concatenation

Just like any other programming language, GDScript, which is a high-level, dynamically-typed programming language used to create content in the Godot engine, makes use of string concatenation. It is a method that links or merges two or more strings.

Learning how to concatenate strings is pivotal as it helps in setting up dialogues in your game, displaying instructions, or even for debugging purposes.

Basic Syntax for String Concatenation

GDScript uses the + operator to concatenate or link two strings. This is a straightforward way to concatenate, and you’ve probably used something similar in other languages.

You can print strings just like numbers:

gdscript
1
2
3
4
5

func _ready():
    var name = "Link"
    print("Welcome, " + name + "!")

Output:

gdscript
1
2
3

Welcome, Link

🔄 Combining Strings and Numbers

You can’t directly combine a string and a number like this:

gdscript
1
2
3

print("Score: " + 100) # ❌ This causes an error!

But you can convert the number to a string using str():

gdscript
1
2
3

print("Score: " + str(100)) # ✅ Works!

🔢 String Length and Access

Want to know how long a string is? Or grab a specific letter?

gdscript
1
2
3
4
5

var phrase = "Godot"
print(phrase.length()) # 5
print(phrase[0])       # G

✂️ Slicing Strings

You can grab parts of a string (called slicing):

gdscript
1
2
3
4

var word = "developer"
print(word.substr(0, 3)) # dev

substr(start, length) cuts out a piece of the string starting at a certain point.


🔁 String Methods You’ll Love

Here are some super useful things you can do with strings:

gdscript
1
2
3
4
5
6
7

var text = "Hello, GODOT!"

print(text.to_lower())  # hello, godot!
print(text.to_upper())  # HELLO, GODOT!
print(text.find("GODOT")) # 7 (starts at index 7)

💡 Why Strings Matter in Games

Strings show up everywhere in game dev:

  • Menus
  • Dialogues
  • Player names
  • Score messages
  • UI labels

Understanding how to use and format strings makes your games more interactive and polished.


✅ Recap

  • A string is text inside quotes ("like this").
  • Combine with +, convert numbers with str().
  • Use .length(), .substr(), and more to work with text.
  • Strings are essential for UI, messages, and player feedback.

SightlessDev