MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

Python, Ruby, Perl: Quoting Strings

Xah Lee, , …,

Python

Strings are enclosed using single quote or double quote. example

a = "this "
b = 'and that'
print a, b

You can use \n for linebreak, and \t for tab, etc.

a = "this\nthat\n" # use \n for line-break
b = 'more\nthings' # single quote works too
print a, b

To quote a string of multiple lines, use triple quotes. Example:

d = """this
will be printed
in 3 lines"""
print d

You can add r in front of the quote symbol. This way, backslash characters will be interpreted as is, not as escapes. (“r” for “raw”)

c = r"this\n and that"
print c # prints a single line

Summary:

Ruby

Ruby's string is very similar to Perl's. Single quote for literal, double quote to eval embedded variable or code.

Use single quote to quote string exactly. (literal string)

# -*- coding: utf-8 -*-
# ruby

aa = 'tiger'   # use single quote for string exactly as is
p aa       # prints 「"tiger"」

# single quoted string containing newline or tab will be printed as they are 
cc = 'a
b'
p cc                            # prints 2 lines

Use double quote for strings that contain newline escape \n, or include variable values or Ruby code.

# -*- coding: utf-8 -*-
# ruby
mm = "tiger\nsnake"
puts mm # prints each word in separate line

You can use \n for newline, and \t for tab, etc.

To evaluate a variable or Ruby code within a string, use #{…}.

# -*- coding: utf-8 -*-
# ruby

# put variable value inside a string
aa = 4
bb = "there are #{aa} tigers"
p bb                         # prints “there are 4 tigers”

# eval Ruby code inside a string
p "there are #{1+2} tigers"                         # prints “there are 3 tigers”

For long string, you can also use:

This is similar to Perl's q{‹…›} and qq{‹…›}. For examples of %q{‹…›} and “heredoc”, see: Ruby: Quoting Long String and Heredoc.

Perl

Use single quote for literal string.

# -*- coding: utf-8 -*-
# perl

# use single quote for literal string
$a = 'this and
 that';
print $a; # prints 2 lines

To have characters \n for newline and \t for tab, use double quote.

# -*- coding: utf-8 -*-
# perl

$a = "this\nand that";
print $a; # prints 2 lines

When double quote is used, variables inside the string will be evaluated.

# -*- coding: utf-8 -*-
# perl

$a = 4;
$b = "this is $a";
print $b; # prints 「this is 4」

Basically, perl has 2 modes of strings: single quote and double quote. In single quote mode, everything is literal. In double quote mode, backslash is a char escape mechanism, and variables inside it will be evaluated.

“q()” and “qq()” Functions

You can also use the syntax q(this n that), which is equivalent to 'this n that'. The parenthesis can be curly brackets {} or square brackets [].

# the following are all same
$a = q(this 'n' that);
$b = q[this 'n' that];
$c = "this 'n' that";
$d = 'this \'n\' that';
print $a, "\n";
print $b, "\n";
print $c, "\n";
print $d, "\n";

Similarly, "…" is same as qq(…).

# -*- coding: utf-8 -*-
# perl

$a = q(everything is literal,
$what or \n ' ' " ");

$b = qq[here, variables $a will be
expanded, backslash act as escape \n (and "quotes" or parenthesis needn't be escaped).];

print $a, "\n";
print '-----------', "\n";
print $b, "\n";
blog comments powered by Disqus