Sunday, May 19, 2013

Strings: indexing, single, double and triple quotes...

For indexing remember that you can index from the start and from the end of a string: 

print s[:2]
print s[:-2]

the triple quotes can be useful in some cases, when you need to include really long strings (e.g. containing several paragraphs of informational text), it is annoying that you have to terminate each line with \n\, especially if you would like to reformat the text occasionally with a powerful text editor like Emacs. For such situations, ``triple-quoted'' strings can be used, e.g.

        hello = """

            This string is bounded by triple double quotes (3 times ").
        Unescaped newlines in the string are retained, though \
        it is still possible\nto use all normal escape sequences.

            Whitespace at the beginning of a line is
        significant.  If you need to include three opening quotes
        you have to escape at least one of them, e.g. \""".

            This string ends in a newline.

No comments:

Post a Comment