10/17/2011 cs lecture strings
TRANSCRIPT
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 1/20
Click to edit Master subtitle style
4/28/12
Do Now: 10 minutes
1. Type the following code into the Idle
text editor (File > New Window):
2. Run the program (Run > RunModule) for various inputs,
including inputs with numbers.
3. Write a one sentence description of what the code does.
print('Hello world!')print(‘Type your name and then press enter.’)myName = input()
print('It is good to meet you, ' + myName)
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 2/20
4/28/12
Homework
• Handout: You may test your answersin Python if you wish
•
Useful Resource: Zelle textbookpages 39 – 43 (PDF pages 47-51)
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 3/20
Click to edit Master subtitle style
4/28/12
Strings
- Chunks of text
- Can store strings with variables, just likewe can store numbers
- Single or double quotes used to identifystrings
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 4/20
Click to edit Master subtitle style
4/28/12
Strings
- Chunks of text
- Can store strings with variables, just likewe can store numbers
- Single or double quotes used to identifystrings
EXAMPLES:greeting = “hello”myName = ‘Matt’myAdvice = “You will not learn
programming in class. You will learnro rammin b writin code, messin u ,
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 5/20
Click to edit Master subtitle style
4/28/12
Characters
-Strings are made up of individual
characters think of a character as anysymbol you can type with a keyboard
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 6/20
Click to edit Master subtitle style
4/28/12
Characters
-Strings are made up of individual
characters think of a character as any
symbol you can type with a keyboard
Examples:LettersSpacesPunctuation
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 7/20
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 8/20
Click to edit Master subtitle style
4/28/12
print() function:
-A function that displays a string on thescreen.
-Ex: print(“hello world”)
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 9/20
Click to edit Master subtitle style
4/28/12
print() function:
-A function that displays a string on thescreen.
-Ex: print(“hello world”)
input() function:
-A function that waits for the user to enter astring and stores the string in a variable.
-Ex: inputString = input()
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 10/20
Click to edit Master subtitle style
4/28/12
len() function
-Identifies the number of characters in astring and stores it in a variable.
-Ex:
myName = “Matt”myNameLength = len(myName)
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 11/20
Click to edit Master subtitle style
4/28/12
len() function
-Identifies the number of characters in astring and stores it in a variable.
-Ex:
myName = “Matt”myNameLength = len(myName) 4
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 12/20
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 13/20
Click to edit Master subtitle style
4/28/12
String concatenation (+ operator)
-Puts strings together to form a singlestring
-Ex:
lastName = “Carlberg ”firstName = “Matthew ”fullName = firstName + lastName
Matthew Carlberg
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 14/20
Click to edit Master subtitle style
4/28/12
Substring
-retrieves specific characters from a string
-Ex:
lastName = “Carlberg”
firstLetter = lastName[0] CsecondLetter = lastName[1] a firstPartOfLastName = lastName[0:4]
Carl
secondPartOfLastName = lastName[4:8]berg
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 15/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes & Noble” bookstoreLength = len(bookstore)
print(bookstore)
print(bookstoreLength)
Output?
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 16/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes & Noble” bookstoreLength = len(bookstore)
print(bookstore)
print(bookStoreLength)
Barnes & Noble14
Output?
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 17/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes\n&\nNoble” bookstoreLength = len(bookstore)
print(bookstore)
print(bookStoreLength)
Output?
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 18/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes\n&\nNoble” bookstoreLength = len(myString)
print(bookstore)
print(bookStoreLength)
Barnes
&Noble14
Output?
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 19/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes & Noble”test1 = bookstore[2:4]
test2 = bookstore[0]
test3 = bookstore[len(bookstore)-1]
crazy = test1 + test2 + “ ” + test3
print(crazy)
Output?
8/3/2019 10/17/2011 CS Lecture Strings
http://slidepdf.com/reader/full/10172011-cs-lecture-strings 20/20
Click to edit Master subtitle style
4/28/12
bookstore = “Barnes & Noble”test1 = bookstore[2:4] rn
test2 = bookstore[0] B
test3 = bookstore[len(bookstore)-1] e
crazy = test1 + test2 + “ ” + test3
print(crazy)
Output?
rnB e