Course of Raku / Essentials / Strings

String concatenation

If you have two strings, you can join them together and get a new longer string. This action is called string concatenation. In Raku, you concatenate strings using—guess what?—concatenation operator. This operator is a tilde: ~. To concatenate two strings, put ~ between them:

say 'Hello, ' ~ 'World!';

If our strings are kept in variables, we can ’concatenate variables‘, well, actually, concatenate the strings that those variables keep:

my $greeting = 'Hello, ';
my $who = 'World!';

say $greeting ~ $who;

Or you can create a new variable using the concatenated value:

my $greeting = 'Hello, ';
my $who = 'World!';
my $message = $greeting ~ $who;

say $message;

Concatenation with assignment

When you need to update the variable and append the new string to it, use the following form:

# Instead of 
$str = $str ~ $another-str;

# use:
$str ~= $another-str;

Practice

Complete the quizzes that cover the contents of this topic.

Course navigation

Strings   |   Strings / Variable interpolation


💪 Or jump directly to the exercises to this section.