Skip to content
Home » PYTHON JUMPSTART COURSE Section 2 – Data Types and Operators, Lesson 3 – Strings

PYTHON JUMPSTART COURSE Section 2 – Data Types and Operators, Lesson 3 – Strings

Spread the love

It’s hard to imagine an application without text. And to use text, we need strings. Strings are not less important in Python than numbers, so let’s explore them in detail. Let’s start with quotes – this is what we need to define strings.

Quotes

We can use single, double or triple quotes for string literals. We use single quotes if we need double quotes inside the string:

We use double quotes if we need single quotes or apostrophes inside the string:

We can also use quotes inside quotes but then they must be escaped with a backslash:

We use triple quotes if the string must span more than one line. These may be triple double quotes:

Indexing Strings

Strings can be indexed in both directions. We use 0-based indexes in the forward direction and -1-based indexes in the backward direction:

indexing strings

We can print a particular character from the string by using its index in square brackets:

If we need the last element of a string, it’s easier to use the -1 index:

We can use other negative indices:

Comparing Strings

We can use all the comparison operators to compare strings. Strings consist of Unicode characters and each character in Unicode is represented by a number and these numbers are compared. It means, the order is not strictly alphabetical, because all capital letters come before small letters in Unicode.

We have the following comparison operators:

comparison operators

Some examples:

These should be not equal:

The first character to differ is the second one. ‘i’ comes after ‘e’:

Capital ‘Y’ comes before small ‘y’:

We can confirm this by means of the ord function, which gives us the Unicode number of the character:

Membership

We can check if a particular element is contained or not in the string. We use the membership operators in and not in:

Immutability

Strings are immutable. Once defined, they can’t be changed:

We can assign a new object to a variable, but this is not changing the string. We are just defining a new string and assigning it to the same variable:

We can’t delete a single character either. If we try to do that, using the del statement, we’ll get an error:

We can delete the whole string:

Slicing

We can slice strings. To do that we specify the startIndex and pastIndex in square brackets. The pastIndex is not included!!!

If we skip startIndex, it means: from the beginning (it’s set to 0), so [:6] is the same as [0:6] :

If we skip pastIndex, it means: to the end:

We can use negative indices as well:

We can copy the whole string slicing the whole of it. We just omit the indices in the square brackets [:]:

We can also slice sequences so that only every third or every fifth element is taken. We just need to set the step as the third argument in the square brackets: [startIndex : pastIndex : step]

Concatenation

We concatenate strings using the + sign:

Repetition

We use the multiplication operator * with strings to repeat them:

Escape Sequences

Escape characters / sequences are characters or sequences used to escape the special meaning of another character.

Examples:

\nnewline

This sequence is used for a new line:

Escape sequences don’t work if we echo strings:

\thorizontal tab

This sequence adds tabs:

\\backslash

This sequence should be used if you need a single backslash:

Raw Strings

If we want to print an escape sequence literally, we can use a raw string. To do so, we have to precede the opening quote with a small or capital letter R:

Otherwise we would get:

Formatting Strings

There are several ways of formatting strings. The two preferable ones are using the format method and f-strings.

The format method takes two kinds of parameters: positional parameters and keyword parameters. Positional parameters are accessed by index, keyword parameters are accessed by keyword. We use placeholders inside the strings.

Let’s start with something simple, a template with just one format field and one positional parameter in the format method:

Here the format code within the curly braces is empty. We could also have more parameters and format fields:

Alternatively, we could use the indices of the positional parameters in the format fields for clarity. We can use them in any order and as many times as we want:

Now, how do we use keyword parameters? Let’s have a look:

We can also mix positional and keyword parameters. Positional parameters must always come first in the method:

And here’s how f-strings work: we have placeholders embedded in the string. Such strings are prefixed with f:

String Methods

There are lots of string methods we can use. If we want to turn all letters to uppercase, we should use the upper method:

If we want to turn all letters to lowercase, we should use the lower method:

The len method returns the length of a string:

The method count counts how many times a substring occurs in a string:

We use the method find to find the index at which a substring is in the string:

Where is “me” in the string?

We use the startswith and endswith methods to check if a string starts or ends with a substring:

The method join takes the string elements from a sequence and concatenates them using the string as the separator:

The method split splits a string into a list of substrings using a delimiter character as the position to split. The delimiter is not included in the result. If no delimiter is given, it defaults to whitespace:

If there are no arguments, the whole string is split on the whitespaces:

Now let’s split on the period:

We can replace substrings with other substrings by using the method replace:

QUIZ

1. Strings are …-based in the forward direction.
    A) 0
    B) 1
    C) -1
 
2. Using the del keyword we can delete:
    A) a single character
    B) part of the string
    C) the whole string
 
3. To escape characters we use the … character:
    A) /
    B) \
    C) r or R
 
4. The horizontal tab escape character is:
    A) \n
    B) \h
    C) \t
 
5. Placeholders are contained in:
    A) parentheses
    B) square brackets
    C) curly braces
 
6. The … method returns the length of a string.
    A) count
    B) len
    C) length
 

TRUE OR FALSE?

1) We can use triple double quotes or triple single quotes.
2) Strings are immutable.
3) We can copy the whole string slicing it with [:].
4) We use the multiplication operator * with strings to repeat them.
5) Escape sequences don’t work if we echo strings.
6) A raw string is a string with suppressed escape sequences.
7) Keyword parameters must always come before positional ones in the format method.
8) The method split splits a string into a list of substrings using a delimiter which is included in the result.

WHAT’S THE OUTPUT?

1)
>>> text = 'I\'d like Jane\'s car.'
>>> text
2)
>>> text = "They said: \"We won't do it.\""
>>> print(text)
3)
>>> name = "Jane"
>>> name[0] + name[-1]
4)
>>> len('hello')
5)
>>> c = "New York"
>>> len(c)
6)
>>> x = "hello"
>>> x[1] + x[-4] + " " + x
7)
>>> "Good" == "good"
8)
>>> "hey" > "hen"
9)
>>> "p" not in "Paris"
10)
>>> word = "interesting"
>>> word[0:2] + " " + word[4:8]
11)
>>> word = "nice"
>>> word[1:]
12)
>>> word = "mustard"
>>> word[:4]
13)
>>> word = "believe"
>>> word[:]
14)
>>> word = "structure"
>>> word[-1:1:-2]
15)
>>> a = "good"
>>> (a[1:3] + "|||") * 3
16)
>>> x = "x"
>>> x *= 4
>>> x
17)
>>> a = "one two \nthree"
>>> print(a)
18)
>>> a = "one two \nthree"
>>> a
19)
>>> a = "one two \tttthree"
>>> print(a)
20)
>>> print(r"My favorite escape characters are \n, \t, \', \" and \\.")
21)
>>> names = ("Harry", "Joe", "Mike")
>>> text = "Her sons are {}, {} and {}.".format(names[2], names[0], names[1])
>>> print(text)
22)
>>> text = "We met {0}, then {1} and finally {2} and {2}'s sister {3}.".format("Jack", "Tom", "Anne", "Natalie")
>>> print(text)
23)
>>> text = "{name}'s {ord}th son lives in {city}.".format(ord = 4, name = "John", city = "Munich")
>>> print(text)
24)
>>> text = "He likes {0} and {1} but he doesn't like {sport}.".format("soccer", "baseball", sport = "rugby")
>>> print(text)
25)
>>> x = 28
>>> f"y = {(x / 7) ** 3}"
26)
>>> a = "come on "
>>> b = a
>>> a.upper() + b
27)
>>> x = 'elle'
>>> x.startswith(x[-1])
28)
>>> x = 'go'
>>> x.startswith("g") == x.endswith("o")
29)
>>> separator = '<>'
>>> print(separator.join("one"))
30)
>>> a = "seven"
>>> text = "The van and other vehicles arrived."
>>> text.split(a[2])
31)
>>> text = "fourth, sixth, seventh, tenth"
>>> text.replace("th", " ")

SOLUTION

QUIZ

1. Strings are …-based in the forward direction.
    A) 0
    B) 1
    C) -1
 
2. Using the del keyword we can delete:
    A) a single character
    B) part of the string
    C) the whole string
 
3. To escape characters we use the … character:
    A) /
    B) \
    C) r or R
 
4. The horizontal tab escape character is:
    A) \n
    B) \h
    C) \t
 
5. Placeholders are contained in:
    A) parentheses
    B) square brackets
    C) curly braces
 
6. The … method returns the length of a string.
    A) count
    B) len
    C) length
 

TRUE OR FALSE?

1) We can use triple double quotes or triple single quotes. True
2) Strings are immutable. True
3) We can copy the whole string slicing it with [:]. True
4) We use the multiplication operator * with strings to repeat them. True
5) Escape sequences don’t work if we echo strings. True
6) A raw string is a string with suppressed escape sequences. True
7) Keyword parameters must always come before positional ones in the format method. False
8) The method split splits a string into a list of substrings using a delimiter which is included in the result. False

WHAT’S THE OUTPUT?

1)
>>> text = 'I\'d like Jane\'s car.'
>>> text


Output:
"I'd like Jane's car."
2)
>>> text = "They said: \"We won't do it.\""
>>> print(text)


Output:
They said: "We won't do it."
3)
>>> name = "Jane"
>>> name[0] + name[-1]


Output:
'Je'
4)
>>> len('hello')

Output:
5
5)
>>> c = "New York"
>>> len(c)


Output:
8
6)
>>> x = "hello"
>>> x[1] + x[-4] + " " + x


Output:
'ee hello'
7)
>>> "Good" == "good"

Output:
False
8)
>>> "hey" > "hen"

Output:
True
9)
>>> "p" not in "Paris"

Output:
True
10)
>>> word = "interesting"
>>> word[0:2] + " " + word[4:8]


Output:
'in rest'
11)
>>> word = "nice"
>>> word[1:]


Output:
'ice'
12)
>>> word = "mustard"
>>> word[:4]


Output:
'must'
13)
>>> word = "believe"
>>> word[:]


Output:
'believe'
14)
>>> word = "structure"
>>> word[-1:1:-2]


Output:
'eucr'
15)
>>> a = "good"
>>> (a[1:3] + "|||") * 3


Output:
'oo|||oo|||oo|||'
16)
>>> x = "x"
>>> x *= 4
>>> x


Output:
'xxxx'
17)
>>> a = "one two \nthree"
>>> print(a)


Output:
one two
three
18)
>>> a = "one two \nthree"
>>> a


Output:
'one two \nthree'
19)
>>> a = "one two \tttthree"
>>> print(a)


Output:
one two    ttthree
20)
>>> print(r"My favorite escape characters are \n, \t, \', \" and \\.")

Output:
My favorite escape characters are \n, \t, \', \" and \\.
21)
>>> names = ("Harry", "Joe", "Mike")
>>> text = "Her sons are {}, {} and {}.".format(names[2], names[0], names[1])
>>> print(text)


Output:
Her sons are Mike, Harry and Joe.
22)
>>> text = "We met {0}, then {1} and finally {2} and {2}'s sister {3}.".format("Jack", "Tom", "Anne", "Natalie")
>>> print(text)


Output:
We met Jack, then Tom and finally Anne and Anne's sister Natalie.
23)
>>> text = "{name}'s {ord}th son lives in {city}.".format(ord = 4, name = "John", city = "Munich")
>>> print(text)


Output:
John's 4th son lives in Munich.
24)
>>> text = "He likes {0} and {1} but he doesn't like {sport}.".format("soccer", "baseball", sport = "rugby")
>>> print(text)


Output:
He likes soccer and baseball but he doesn't like rugby.
25)
>>> x = 28
>>> f"y = {(x / 7) ** 3}"


Output:
'y = 64.0'
26)
>>> a = "come on "
>>> b = a
>>> a.upper() + b


Output:
'COME ON come on '
27)
>>> x = 'elle'
>>> x.startswith(x[-1])


Output:
True
28)
>>> x = 'go'
>>> x.startswith("g") == x.endswith("o")


Output:
True
29)
>>> separator = '<>'
>>> print(separator.join("one"))


Output:
o<>n<>e
30)
>>> a = "seven"
>>> text = "The van and other vehicles arrived."
>>> text.split(a[2])


Output:
['The ', 'an and other ', 'ehicles arri', 'ed.']
31)
>>> text = "fourth, sixth, seventh, tenth"
>>> text.replace("th", " ")


Output:
'four , six , seventh, ten'

PROJECT

PROJECT SOLUTION


Spread the love

Leave a Reply