Strings C-START Python PD Workshop C-START Python PD Workshop - - PowerPoint PPT Presentation

strings
SMART_READER_LITE
LIVE PREVIEW

Strings C-START Python PD Workshop C-START Python PD Workshop - - PowerPoint PPT Presentation

Strings C-START Python PD Workshop C-START Python PD Workshop Strings Special Characters \t C-START Python PD Workshop "Glow in the Dark" Favorite Color: print("Favorite Color: \n\t\" Glow in the Dark \" ")


slide-1
SLIDE 1

Strings

C-START Python PD Workshop

C-START Python PD Workshop Strings

slide-2
SLIDE 2

Special Characters

Special characters can be inserted in a string using an escape sequence: a backslash (\) followed by another character. Here are some common escape sequences: \" Double Quote \\ Backslash \n Newline \t Horizontal Tab Here is an example of using some escape sequences: print("Favorite Color:\n\t\"Glow in the Dark\"") Favorite Color: "Glow in the Dark"

C-START Python PD Workshop Strings

slide-3
SLIDE 3

Special Characters

Special characters can be inserted in a string using an escape sequence: a backslash (\) followed by another character. Here are some common escape sequences: \" Double Quote \\ Backslash \n Newline \t Horizontal Tab Here is an example of using some escape sequences: print("Favorite Color:\n\t\"Glow in the Dark\"") Favorite Color: "Glow in the Dark"

C-START Python PD Workshop Strings

slide-4
SLIDE 4

Single or Double Quotes: Your Choice

Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.")

C-START Python PD Workshop Strings

slide-5
SLIDE 5

Single or Double Quotes: Your Choice

Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.")

C-START Python PD Workshop Strings

slide-6
SLIDE 6

Single or Double Quotes: Your Choice

Strings can be written using either single or double quotes, your choice. primary = 'Python' secondary = "English" Using single quotes means no need to escape double quotes: print('So you must be "the one"?') Using double quotes means no need to escape single quotes: print("Margaret's house is blue.")

C-START Python PD Workshop Strings

slide-7
SLIDE 7

Strings Are Like Lists

Strings are like lists containing characters: myname = "Jack" print(myname[0]) J But unlike lists, strings cannot be modifjed: myname = "Jack" myname[0] = "T" # bad

C-START Python PD Workshop Strings

slide-8
SLIDE 8

Strings Are Like Lists

Strings are like lists containing characters: myname = "Jack" print(myname[0]) J But unlike lists, strings cannot be modifjed: myname = "Jack" myname[0] = "T" # bad

C-START Python PD Workshop Strings

slide-9
SLIDE 9

Strings are Iterables!

for c in 'hello world': print(c) h e l l

  • w
  • r

l d

C-START Python PD Workshop Strings

slide-10
SLIDE 10

.split()ting Strings

To separate a string into a list based on white spaces, call .split() on it. Here is an example: my_str = " Python is really cool" wordlist = my_str.split() # wordlist will be ["Python", "is", ... ] for word in wordlist: print(word) Python is really cool

C-START Python PD Workshop Strings

slide-11
SLIDE 11

.split()ting Strings

To separate a string into a list based on white spaces, call .split() on it. Here is an example: my_str = " Python is really cool" wordlist = my_str.split() # wordlist will be ["Python", "is", ... ] for word in wordlist: print(word) The . Operator The . operator used above is actually the accessor operator, however, most programmers simply call it the dot operator. It allows us to use a function which is specifjc to a certain data type

  • n the object.

C-START Python PD Workshop Strings

slide-12
SLIDE 12

Splitting the Input

Remember that the input function returns a string contaning the line that the user typed. If we want to accept multiple words per line, we must split the input. line = input("What is your full name? ") words = line.split() firstname = words[0] lastname = words[1] Useful for Kattis Some Kattis problems require that you recieve input on a single line separated by spaces. This is an efgective method to receive the input.

C-START Python PD Workshop Strings

slide-13
SLIDE 13

Splitting the Input

Remember that the input function returns a string contaning the line that the user typed. If we want to accept multiple words per line, we must split the input. line = input("What is your full name? ") words = line.split() firstname = words[0] lastname = words[1] Useful for Kattis Some Kattis problems require that you recieve input on a single line separated by spaces. This is an efgective method to receive the input.

C-START Python PD Workshop Strings