Strings

Strings represent text. The way to write them changes if you want to insert values or not.

Double quotes#

Between double quotes you can interpolate values: $variable or ${expression}:

name = "Ana"
balance = 100
tax = 15
print "Hello $name, you have ${balance - tax} dollars."
Context-sensitive dollar

The dollar $ indicates interpolation, but only when followed by a valid identifier or an expression between braces:

price = 10.5

print "This dollar $ is literal"  # This dollar $ is literal

print "Thanks for donating $10"   # Thanks for donating $10

# The first dollar is literal
print "The price is $$price"    # The price is $10.5

Escape sequences#

In double quotes these escape sequences are allowed:

SequenceResult
\nLine break
\tTabulation
\rCarriage return
\"Literal double quote
\$Literal dollar
\\Literal backslash
print "First line\nSecond line"
print "Column 1\tColumn 2"
print "She said: \"Hello\""

To include a double quote, you can also repeat it "" without needing escape:

print "She said: ""Hello"""   # She said: "Hello"
What happens if I escape another character?

If you use \ before a character that is not part of the valid sequences, it is interpreted literally. For example, \x remains as \x in the resulting string.

Single quotes#

The content is taken as is: they ignore interpolation and escape sequences.

path = 'C:\Windows\System32\cmd.exe'
print '$variable is not interpreted here'
print 'This is a literal \n without escaping'

To include a single quote, repeat it twice:

print 'You can '' do this'  # You can ' do this
Which to use?
  • Double when you need flexibility. Single for paths, regex patterns or text where you want everything to be literal.
  • For multiline literals, consider Templates.