Module05 Strings
Module05 Strings
MODULE 5
STRINGS
Strings
> Python has powerful and flexible built-in string processing capabilities
> You can write string literals using either single quotes ' or double quotes ":
> For multiline strings with line breaks, you can use triple quotes, either ''' or """:
> The line breaks after """ and after lines are included
Strings
> Python strings are immutable; you cannot modify a string:
Strings
> Strings are a sequence of Unicode characters and can be treated like other
sequences, e.g. lists and tuples:
slicing
Strings
> The backslash character \ is an escape character
> It is used to specify special characters like newline \n or Unicode characters.
> To write a string literal with backslashes, you need to escape them:
Strings
> If you have a string with a lot of backslashes and no special characters, you
might find this a bit annoying.
> Fortunately, you can preface the leading quote of the string with r
– The r stands for raw
– It means that the characters should be interpreted as is:
Strings
> Adding two strings together concatenates them and produces a new string:
Strings
> String templating or formatting is important
> String objects have a format method
– Used to substitute formatted arguments into the string, producing a new
string:
> You can convert this Unicode string to its UTF-8 bytes representation using the
encode method
Bytes and Unicode
> Assuming you know the Unicode encoding of a bytes object, you can go back
using the decode method:
> While it’s become preferred to use UTF-8 for any encoding, for historical
reasons you may encounter data in any number of different encodings:
> If you specify as the second argument a start_index, count searches only the
slice string[start_index:]—that is, from start_index through end of the
string:
> String methods find and rfind perform the same tasks as index and rindex
but, if the substring is not found, return -1 rather than causing a ValueError.
Replacing Substrings
> A common text manipulation is to locate a substring and replace its value.
> Method replace takes two substrings.
> It searches a string for the substring in its first argument and replaces each
occurrence with the substring in its second argument.
> The method returns a new string containing the results.
Removing Leading and Trailing Whitespace
Joining Strings
partition
> String method partition splits a string into a tuple of three strings based on
the method’s separator argument.
– the part of the original string before the separator,
– the separator itself,
– the part of the string after the separator.
partition
splitlines
> Method splitlines returns a list of new strings representing the lines of text
split at each newline character in the original string.