ITEC 109 Lecture 13 Strings Review Input Computation

  • Slides: 18
Download presentation
ITEC 109 Lecture 13 Strings

ITEC 109 Lecture 13 Strings

Review • Input + Computation + Output Strings 1

Review • Input + Computation + Output Strings 1

Objectives • Functions calls for strings in python • Working with text Strings 1

Objectives • Functions calls for strings in python • Working with text Strings 1

Representatio n temp = “Hello World”; H e 0 1 l 2 l o

Representatio n temp = “Hello World”; H e 0 1 l 2 l o 3 4 print. Now(temp[0]) print. Now(temp[0: 4]) 5 W o 6 7 r 8 l d 9 Single character Range of characters Strings 1 10

Variables • Collection of characters (array) x = “Hi You” • Access parts of

Variables • Collection of characters (array) x = “Hi You” • Access parts of the variable First = x[0: 1] print. Now(First) Strings 1

Function calls • • How big is it? Where is X character? Upper or

Function calls • • How big is it? Where is X character? Upper or lower case it Replace characters in the string Strings 1

Motivation John Doe|Debt|300 Credit card bill: Dear John, Strings 1 You owe us $300.

Motivation John Doe|Debt|300 Credit card bill: Dear John, Strings 1 You owe us $300.

Functions str = “Hello World”; • index and length H e 0 1 l

Functions str = “Hello World”; • index and length H e 0 1 l 2 l o 3 4 5 num = index(‘ ‘); num 2 = len(str) result = str[num+1: num 2] Strings 1 W o 6 7 r 8 l d 9 10

More str = “Hello World”; • Upper case str = str. upper(); • Lower

More str = “Hello World”; • Upper case str = str. upper(); • Lower case str = str. lower(); • Replace str = str. replace(‘H’, ’W’); Method call, similar to function call (OO style vs procedural) Strings 1

Advanced test="Hello|World|End" num = test. find('|') first = test[0: num] num 2 = test.

Advanced test="Hello|World|End" num = test. find('|') first = test[0: num] num 2 = test. find('|', num+1) second = test[num+1: num 2] third = test[num 2+1: len(test)] print. Now(first+second+third) Strings 1 Hello Get the 2 nd bar World End

Functions def parse. AString(name): num = name. find(‘|’) one = name[0: num] two =

Functions def parse. AString(name): num = name. find(‘|’) one = name[0: num] two = name[num+1: len(name)] print. Now(one +” “ + two) parse. AString(“Hello|World”) parse. AString(“One|Two”) Strings 1

Conditionals a="one" b="two" if (a != b): print. Now("Test") elif (a == b): print.

Conditionals a="one" b="two" if (a != b): print. Now("Test") elif (a == b): print. Now(“Equal”) else: print. Now(“End of the world”) Strings 1

Loops counter=0 source="World" target="" while (counter < 4): target = target+source counter = counter

Loops counter=0 source="World" target="" while (counter < 4): target = target+source counter = counter +1 print. Now(target) Strings 1

Example • Find the first character in the first name and store it in

Example • Find the first character in the first name and store it in a string called formatted • Print out “Welcome” then print out the value of formatted then a space then the last name Andrew Ray Strings 1 A. Ray

Backgroun d Strings 1

Backgroun d Strings 1

Methods • • Any that you remember? Caesar A->C, B->D, etc… Substitution – Replace

Methods • • Any that you remember? Caesar A->C, B->D, etc… Substitution – Replace A with 7 – Replace E with 3 Strings 1

Sample • What code would you need to write to create this encryption ?

Sample • What code would you need to write to create this encryption ? Strings 1

Summary • Strings – Methods – Functions – Conditionals – Loops Strings 1

Summary • Strings – Methods – Functions – Conditionals – Loops Strings 1