0% found this document useful (0 votes)
25 views

Module05 Strings

Strings can represent text and are immutable sequences of characters. They have many useful methods for operations like searching, splitting, joining, formatting and case changes. Common string methods include find, replace, lower, upper, count, startswith, endswith and partition.

Uploaded by

efeerer582
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Module05 Strings

Strings can represent text and are immutable sequences of characters. They have many useful methods for operations like searching, splitting, joining, formatting and case changes. Common string methods include find, replace, lower, upper, count, startswith, endswith and partition.

Uploaded by

efeerer582
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Computer Programming I

Binnur Kurt, PhD

[email protected]

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:

> After this operation, the variable a is unmodified:


Strings
> Many Python objects can be converted to a string using the str function:

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:

– {0:.2f} means to format the first argument as a floating-point number with


two decimal places.
– {1:s} means to format the second argument as a string.
– {2:d} means to format the third argument as an exact integer.
Strings
> To substitute arguments for these format parameters, pass a sequence of
arguments to the format method:

> String formatting is a deep topic


– there are multiple methods and numerous options
– tweaks available to control how values are formatted in the resulting string
– To learn more, consult the official Python documentation:
https://docs.python.org/3/

Bytes and Unicode


> In modern Python (Python 3.0+), Unicode has become the first-class string type
to enable more consistent handling of ASCII and non-ASCII text.

> 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:

Bytes and Unicode


> It is most common to encounter bytes objects in the context of working with
files, where implicitly decoding all data to Unicode strings may not be desired.
> Though you may seldom need to do so, you can define your own byte literals
by prefixing a string with b:
Comparison Operators for Strings
> Strings may be compared with the comparison operators.
> Recall that strings are compared based on their underlying integer numeric
values.
> So uppercase letters compare as less than lowercase letters because uppercase
letters have lower integer values.

Comparison Operators for Strings


> Let’s compare the strings 'Orange' and 'orange' using the comparison
operators:
Searching for Substrings
> String method count returns the number of times its argument occurs in the
string on which the method is called

> 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:

Searching for Substrings


> If you specify as the second and third arguments the start_index and
end_index, count searches only the slice
string[start_index:end_index]—that is, from start_index up to, but
not including, end_index:
Locating a Substring in a String
> String method index searches for a substring within a string and returns the
first index at which the substring is found; otherwise, a ValueError occurs

> 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.

Determining Whether a String Contains a Substring


> If you need to know only whether a string contains a substring, use operator in
or not in
Locating a Substring at the Beginning or End of a String
> String methods startswith and endswith return True if the string starts
with or ends with a specified substring:

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

Changing Character Case


Splitting Strings

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.

Characters and Character-Testing Methods


Characters and Character-Testing Methods
String Method Description
isalnum() Returns True if the string contains only alphanumeric characters (i.e.,
digits and letters).
isalpha() Returns True if the string contains only alphabetic characters (i.e., letters).
isdecimal() Returns True if the string contains only decimal integer characters (that is,
base 10 integers) and does not contain a + or - sign.
isdigit() Returns True if the string contains only digits (e.g., '0', '1', '2').
isidentifier() Returns True if the string represents a valid identifier.
islower() Returns True if all alphabetic characters in the string are lowercase
characters (e.g., 'a', 'b', 'c').
isnumeric() Returns True if the characters in the string represent a numeric value
without a + or - sign and without a decimal point.
isspace() Returns True if the string contains only whitespace characters.

Characters and Character-Testing Methods


String Method Description
istitle() Returns True if the first character of each word in the string is the only
uppercase character in the word.
isupper() Returns True if all alphabetic characters in the string are uppercase
characters (e.g., 'A', 'B', 'C').

You might also like