Posts Tagged ‘concatenation’
Perl Strings
15/03/2011
Leave a comment
'Hello World!' # Returns 'Hello World!', supports escaping of ' and \ 'It\'s\\It is' # Returns 'It's\It is' 'Don\'t "escape"' # Returns 'Don't "escape"' 'Hello\nWorld!' # Returns 'Hello\nWorld!'
"Hello World!" # Returns 'Hello World!', supports escaping multiple # characters, including \n and \t, and interpolation "Hello\nWorld!" # Returns 'Hello # World!'
'Hello' . ' ' . 'World!' # Returns 'Hello World!'
$hi = "Hello"; # The two types of interpolation are equivalent. The first type is used # for variable names that would cause ambiguity. "${hi} World!" # Returns 'Hello World!' "$hi World!" # Returns 'Hello World!'
Categories: Code, Perl, Programming for Programmers
concatenation, interpolation, perl, strings
Python Strings
14/03/2011
3 comments
A ‘ and ” are interpreted as being the same in Python. “”” delimits a multi-line string. There is no reason to use one style of string delimiter over the other, although I personally follow the style presented by Will Harris in StackOverflow (see bottom for summary).
'Hello World!' # Returns 'Hello World!' "Hello World!" # Returns 'Hello World!' """Hello World!""" # Returns 'Hello World!' 'Hello # Error: ' doesn't support unescaped newlines World!' 'Hello\nWorld!' # Returns 'Hello\nWorld!' "Hello # Error: " doesn't support unescaped newlines World!" "Hello\nWorld!" # Returns 'Hello\nWorld!' """Hello # Returns 'Hello\nWorld!' World!""" """Hello\nWorld!""" # Returns 'Hello\nWorld!' 'Hello' + ' ' + 'World!' # Returns 'Hello World!' '%s %s!' % ('Hello', 'World') # Interpolation, returns 'Hello World!'
Will Harris’ style for Python string, summary
- Single quotes for small symbol-like strings.
- Double quotes around strings that are used for interpolation or that are natural language messages.
- Triple double quotes for docstrings and raw string literals for regular expressions.
Categories: Code, Programming for Programmers, Python
concatenation, convention, interpolation, python, stackoverflow, strings