Basics · lesson 4 of 18

Strings

Concatenate with ~, repeat with x, interpolate whole expressions, and call string methods.

Strings concatenate with ~ and repeat with x:

say 'Rak' ~ 'u';
say 'ha' x 3;
Output
Raku
hahaha

Methods #

Like every value in Raku, a string carries its own methods — call them with the dot:

my $word = 'Camelia';
say $word.chars;
say $word.uc;
say $word.flip;
say 'the quick brown fox'.words.elems;
Output
7
CAMELIA
ailemaC
4

.chars counts characters, .uc upcases, .flip reverses, and .words splits on whitespace (here we count the pieces with .elems).

Interpolating expressions #

Double quotes interpolate more than variables: a { ... } block runs any code and drops its result into the string.

my $n = 6;
say "$n squared is { $n ** 2 }.";
say "Today's word, backwards: { 'Raku'.flip }";
Output
6 squared is 36.
Today's word, backwards: ukaR

Try it #

Print your name in uppercase and reversed — ANNA stays suspiciously readable both ways.

my $name = 'Anna';
Show a solution
my $name = 'Anna';
say $name.uc;
say $name.uc.flip;
Output
ANNA
ANNA