CS计算机代考程序代写 python Digression: Scoring Matrices

Digression: Scoring Matrices

Lecture 10
Strings

L10 Strings – 2

Objectives

• To understand the string data type and how
strings are represented in a computer.

• To understand the basic idea of sequences and
indexing as they apply to Python strings and
lists.

L10 Strings – 3

The String Data Type

• The most common use of personal computers
is word processing.

• Text is represented in programs by the string
data type.

• A string is a sequence of characters enclosed
within quotation marks (“) or apostrophes (‘).

L10 Strings – 4

The String Data Type

>>> str1=”Hello”

>>> str2=’spam’

>>> print(str1, str2)

Hello spam

>>> type(str1)

>>> type(str2)

L10 Strings – 5

The String Data Type

• Getting a string as input

>>> firstName = input(“Please enter your name: ”)

Please enter your name: John

>>> print(“Hello”, firstName)

Hello John

• Notice that the input is not evaluated. We want to
store the typed characters, not to evaluate them as
a Python expression, e.g. convert to int.

L10 Strings – 6

The String Data Type

• We can access the individual characters in a
string through indexing.

• The positions in a string are numbered from
the left, starting with 0.

• The general form is [] where
the value of expr (i.e. an integer) determines
which character is selected from the string.

L10 Strings – 7

The String Data Type

H e l l o B o b

0 1 2 3 4 5 6 7 8

>>> greet = “Hello Bob”

>>> greet[0]

‘H’

>>> print(greet[0], greet[2], greet[4])

H l o

>>> x = 8

>>> print(greet[x – 2])

B

L10 Strings – 8

The String Data Type

H e l l o B o b

0 1 2 3 4 5 6 7 8
• In a string of n characters, the last character

is at position n-1 since we start counting
with 0.

• We can index from the right side using
negative indexes.

>>> greet[-1]

‘b’

>>> greet[-3]

‘B’

L10 Strings – 9

The String Data Type

• Indexing returns a string containing a single
character from a larger string.

• We can also access a contiguous sequence of
characters, called a substring, through a
process called slicing.

L10 Strings – 10

Slicing Strings

changhsinlee.com

L10 Strings – 11

The String Data Type

• Slicing:
[:]

• start and end should both be ints

• The slice contains the substring beginning at
position start and runs up to but does NOT
include the position end.

L10 Strings – 12

The String Data Type

H e l l o B o b

0 1 2 3 4 5 6 7 8
>>> greet[0:3]

‘Hel’

>>> greet[5:9]

‘ Bob’

>>> greet[:5]

‘Hello’

>>> greet[5:]

‘ Bob’

>>> greet[:]

‘Hello Bob’

This is same as greet

L10 Strings – 13

The String Data Type

• If either start or end expression is missing, then the
start or the end of the string are used.

• Can we put two strings together into a longer
string?

• Concatenation “glues” two strings together (+)

• Repetition builds up a string by multiple
concatenations of a string with itself (*)

L10 Strings – 14

The String Data Type

:

L10 Strings – 15

The String Data Type

>>> “spam” + “eggs”

‘spameggs’

>>> “Spam” + “And” + “Eggs”

‘SpamAndEggs’

>>> 3 * “spam”

‘spamspamspam’

>>> “spam” * 5

‘spamspamspamspamspam’

>>> (3 * “spam”) + (“eggs” * 5)

‘spamspamspameggseggseggseggseggs’

L10 Strings – 16

The String Data Type

The function len() is used to return the length of string.

>>> len(“spam”)

4

>>> for ch in “Spam!”:

print(ch, end=” “)

S p a m !

Note: ‘ ’ printed after
each character value

L10 Strings – 17

Summary

• We learned how strings are represented in a
computer.

• We learned about substrings and string slicing.

• We learned how various operations can be
performed on strings.

Lecture 10�Strings
Objectives
The String Data Type
The String Data Type
The String Data Type
The String Data Type
The String Data Type
The String Data Type
The String Data Type
Slicing Strings
The String Data Type
The String Data Type
The String Data Type
The String Data Type
The String Data Type
The String Data Type
Summary