0% found this document useful (0 votes)
56 views22 pages

Section1 4

python

Uploaded by

Ff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views22 pages

Section1 4

python

Uploaded by

Ff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Strings

Section 2
Chapter 2

An Introduction to Programming Using Python


David Schneider
© 2016 Pearson Education, Ltd. All rights reserved.
String Literals
• Sequence of characters that is treated as a
single item
• Written as a sequence of characters
surrounded by either single quotes (') or
double quotes (").

Opening and closing


quotation marks must
be the same type

© 2016 Pearson Education, Ltd. All rights reserved.


String Variables

• Variables also can be assigned string values


• Created (come into existence) the first time
they appear in assignment statements
• When an argument of a print statement
– Quotation marks not included in display

© 2016 Pearson Education, Ltd. All rights reserved.


Indices and Slices
• Position or index of a character in a string
– Identified with one of the numbers 0, 1, 2, 3, . . . .

FIGURE 2.4 Indices of the characters of


the string "spam & eggs".
© 2016 Pearson Education, Ltd. All rights reserved.
Indices and Slices
• If str1 is a string, then str1[m:n] is the
substring beginning at position m and ending
at position n - 1
– Example “spam & eggs”[2:7]

FIGURE 2.5 Aid to visualizing slices.

© 2016 Pearson Education, Ltd. All rights reserved.


Indices and Slices
• Example 1: Program shows use of indices

© 2016 Pearson Education, Ltd. All rights reserved.


Negative Indices
• Python allows strings to be indexed by their
position with regards to the right
– Use negative numbers for indices.

FIGURE 2.6 Negative indices of the characters of


the string "spam & eggs".
© 2016 Pearson Education, Ltd. All rights reserved.
Negative Indices
• Example 2: Program illustrates negative
indices.

© 2016 Pearson Education, Ltd. All rights reserved.


Default Bounds for Slices
• Example 3: Program illustrates default
bounds

© 2016 Pearson Education, Ltd. All rights reserved.


String Concatenation
• Two strings can be combined to form a new
string
– Consisting of the strings joined together
– Represented by a plus sign
• Combination of strings, plus signs, functions,
and methods can be evaluated
– Called a string expression

© 2016 Pearson Education, Ltd. All rights reserved.


String Repetition
• Asterisk operator can be used with strings to
repeatedly concatenate a string with itself

© 2016 Pearson Education, Ltd. All rights reserved.


String Functions and Methods

Table 2.3 String Operations (str1 = "Python")

© 2016 Pearson Education, Ltd. All rights reserved.


Chained Methods
• Lines can be combined into a single line said
to chain the two methods
– Executed from left to right

© 2016 Pearson Education, Ltd. All rights reserved.


The input Function
• Prompts the user to enter data

– User types response, presses ENTER key


– Entry assigned to variable on left

© 2016 Pearson Education, Ltd. All rights reserved.


The input Function
• Example 4: Program parses a name

© 2016 Pearson Education, Ltd. All rights reserved.


More String Functions
• Example 5:
Program
shows use
of int, float,
and eval
functions

© 2016 Pearson Education, Ltd. All rights reserved.


String Functions with Numbers

• int and float also work with numbers


• The str function converts a number to its
string representation

© 2016 Pearson Education, Ltd. All rights reserved.


Internal Documentation
Benefits of documentation
1.Other people easily understand program.
2.You can better understand program when you
read it later.
3.Long programs are easier to read
– Purposes of individual pieces can be determined
at a glance.

© 2016 Pearson Education, Ltd. All rights reserved.


Internal Documentation
• Example 6: Program shows use of
documentation.

© 2016 Pearson Education, Ltd. All rights reserved.


Line Continuation
• A long statement can be split across two or
more lines
– End each line with backslash character ( \ )
• Alternatively any code enclosed in a pair of
parentheses can span multiple lines.
– This is preferred style for most Python
programmers

© 2016 Pearson Education, Ltd. All rights reserved.


Indexing and Slicing Out of Bounds
• Python does not allow out of bounds indexing
for individual characters of strings
– Does allow out of bounds indices for slices
• Given: str1 = “Python”
– Then print(str1[7]) print(str1[-7])

– These are OK

© 2016 Pearson Education, Ltd. All rights reserved.


End

Section 2
Chapter 2

An Introduction to Programming Using Python


David Schneider
© 2016 Pearson Education, Ltd. All rights reserved.

You might also like