/
Strings The Basics Strings Strings The Basics Strings

Strings The Basics Strings - PowerPoint Presentation

tawny-fly
tawny-fly . @tawny-fly
Follow
369 views
Uploaded On 2018-02-26

Strings The Basics Strings - PPT Presentation

a collection data type can refer to a string variable as one variable or as many different components characters string values are delimited by either single quotes or double quotes operators ID: 636377

characters string return strings string characters strings return ascii integer len variable ord chr notation function individual python character

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Strings The Basics Strings" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Strings

The BasicsSlide2

Strings

a collection data type

can refer to a string variable as one variable or as many different components (characters)

string values are delimited by either single quotes or double quotes

operators

+ and * (overloaded operators)

+ “concatenation” sticks two strings together to get a new string

* “replication” repeats a string a given number of times to give a new string

precedence is * above

+

order of concatenation matters! “a” + “b” is not the same as “b” + “a”Slide3

Indexing

Also called subscripts

A way to tell individual characters of a string apart

notation like that used for lists

[ ] with an integer (constant or variable) inside

Strings are numbered from 0 on left end and increasing

Strings are

also

numbered from -1 on right end and

decreasing

You can use an expression as an index also, like

str

[k + 1], as long as the expression has an integer valueSlide4

The len function

Note this is a function, not a method (do not call it with the dot notation)

len operates on one argument, either a string or a list

returns an integer result, tells

how many

characters are in the string or how many elements are in the list

lowest return value is zero for the empty string

Python claims “no upper limit on string length”, literally it depends on your environment – how much RAM you have, which OS you are running, on a typical PC today with Windows it is around 2 billion characters

s[len(s)] would give an error! Remember that len tells you

how many

characters, not what the last subscript in the string would beSlide5

chr and

ord

functions

Sometimes you need to work with the ASCII codes of individual characters

chr

(integer) will return the character corresponding to the ASCII code argument, example

chr

(65) will return “A”

ord

(char) will return the ASCII code (as an integer) of the single character that you send, example

ord

(“A”) will return 65

In general, use the characters instead of their ASCII values in coding, it will be much more obvious what you are doing

D

o NOT say “if

ch

>= 65 and

ch

<= 90” when you are checking for upper case

it is MUCH better to say

if

ch

>= “A” and

ch

<=“Z”

or even

if

ch

in

ascii_uppercase

or

if

ch.isupper

()

Slide6

The slice operator

The “substring” operator in Python

See separate set of slides