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

CHP 5: Python

1) Strings can be defined using single or double quotes and represent a sequence of characters that is treated as a single item. 2) String values can be assigned to variables and printed without the enclosing quotation marks. 3) Individual characters in a string can be accessed using indices, and slices of strings can be accessed using index ranges. 4) Strings support operations like concatenation, repetition, and various functions that parse and format string values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CHP 5: Python

1) Strings can be defined using single or double quotes and represent a sequence of characters that is treated as a single item. 2) String values can be assigned to variables and printed without the enclosing quotation marks. 3) Individual characters in a string can be accessed using indices, and slices of strings can be accessed using index ranges. 4) Strings support operations like concatenation, repetition, and various functions that parse and format string values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

BIC3104/CC303

Chapter 5
Strings
String Literals

Written as a sequence of
characters surrounded by
Sequence of characters that is either single quotes (') or
treated as a single item 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.5Aid 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.


© 2 0 1 6 P E AR S O N E D U C AT I O N , LT D . AL L R I GH TS
R E S E RV E D .

Consisting of
Two strings can be the strings
combined to form a joined together
new string Represented by
a plus sign

String
Concatenation Combination of strings,
plus signs, functions, Called a string
and methods can be expression
evaluated
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.

You might also like