LessonsBeginner: First stepsLesson 3

Joining text

Build sentences out of pieces with & and &&, and use xTalk's names for spaces, tabs and new lines.

  • Beginner
  • Lesson 3 of 83
  • About 7 minutes
  • 5 examples

Two ways to join

Joining strings together is called concatenation. xTalk has two operators for it: & joins two values exactly as they are, and && joins them with a space between.

ExampleRun it in the playground
put "sun" & "flower"
put "sun" && "flower"
put "Room" && 101
put "Total:" && 6 * 7 && "points"
Output
sunflower
sun flower
Room 101
Total: 42 points

Numbers join just like text, and a sum is worked out before it is joined. You can chain as many pieces as you like on one line.

Names for characters you cannot see

Some characters are hard to type inside quotes. xTalk gives them names, called constants, which you join like any other value:

ConstantIs
spaceone space (&& is a shortcut for & space &)
taba tab character, for columns
return (or cr)a new line
comma, colon, slash, : /
quotea double quote, "
emptynothing at all, the same as ""
ExampleRun it in the playground
put "Line one" & return & "Line two"
put "Name" & tab & "Score"
put "a" & comma & "b" & comma & "c"
put "Say" && quote & "cheese" & quote
Output
Line one
Line two
Name	Score
a,b,c
Say "cheese"

Notice that the first put made two lines of Output: the value itself has a new line in it.

Building a string a piece at a time

Long text is often built in a variable, a piece at a time, with put ... after (and put ... before for the front). Variables come in the next lesson, but the idea is simple: tLine is a named place to keep the string while it grows.

ExampleRun it in the playground
put "Dear" into tLine
put space & "Ada" after tLine
put "," after tLine
put "Hello! " before tLine
put tLine
Output
Hello! Dear Ada,

Numbers inside text

When you join a number to text, its digits become part of the string. Be careful with sums: brackets make sure the sum happens before the join.

ExampleRun it in the playground
put "2 + 3 =" && 2 + 3
put "Ages:" && (20 + 1) & "," && (30 + 1)
put 1 & 2 + 3
Output
2 + 3 = 5
Ages: 21, 31
15

The last line shows why brackets help: + is worked out before &, so xTalk adds 2 and 3 first and then joins the 1 in front, giving the string 15.

Try it yourself

Make a single put show a two-line label: Name: Grace Hopper on the first line and Born: 1906 on the second. Use &&, & and return.

Show a solution
ExampleRun it in the playground
put "Name:" && "Grace" && "Hopper" & return & "Born:" && 1906
Output
Name: Grace Hopper
Born: 1906

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.