Download presentation
Presentation is loading. Please wait.
Published byJohnathan Briggs Modified over 9 years ago
1
Strings CS303E: Elements of Computers and Programming
2
The String Data Type Represents text Represents text So far, we’ve used it for input and output So far, we’ve used it for input and output Now, we’ll take a closer look: Now, we’ll take a closer look: –Representation –String operations Like arithmetic operations, but different Like arithmetic operations, but different
3
String Representation Defined with single or double quotes Defined with single or double quotes –Need to start and end with same type of quote firstName=‘Steve’ firstName=‘Steve’ lastName=“Jobs” lastName=“Jobs” Represented as a sequence of characters Represented as a sequence of characters Steve
4
Strings as Input Get using raw_input() Get using raw_input() OR input(), but then the user should include the text in quotation marks OR input(), but then the user should include the text in quotation marks Best to use raw_input() and not expect the user to know to use quotation marks Best to use raw_input() and not expect the user to know to use quotation marks
5
String Concatenation “Glue” two strings together to form a new string using + “Glue” two strings together to form a new string using +Example: myString=“call ”+”911” print myString Output: call 911
6
String Concatenation + is overloaded in Python + is overloaded in Python –Math and strings Determines whether to add or concatenate based on the first operand Determines whether to add or concatenate based on the first operand – + … indicates math – + … indicates concatenation –canNOT combine the two
7
String Concatenation But what if you want to combine the two? But what if you want to combine the two? –Convert numbers to strings using str() To convert a string to a number: To convert a string to a number: –Use int(), float(), etc –Or eval(), which evaluates an expression in a string and converts it to a number
8
String Repetition Use * to repeat a string any number of times Use * to repeat a string any number of times –Concatenate a string with itself Example: print “Hello”*3 Output: HelloHelloHello * is also overloaded * is also overloaded
9
String Membership You can test to see if a string is a member of a larger string You can test to see if a string is a member of a larger stringExample: myString = “Hello” print “el” in myString Output:True
10
Question: String Concatenation Which is an invalid expression? Which is an invalid expression? A. 54 + 12 B. “a” + “b” C. “7” + “car” D. “hello” + 4
11
String Length Recall that strings are sequences of characters Recall that strings are sequences of characters Use len() to count those characters Use len() to count those characters –Returns the length of the string –Includes all characters---even the spaces
12
String Length Example: myString=“Hello, World” print “The length of “ + myString + “ is ” + len(myString) Output: The length of Hello, World is 12
13
String Indexing Use indexing to access the individual characters in a string Use indexing to access the individual characters in a string Characters are numbered, or indexed, beginning at 0 Characters are numbered, or indexed, beginning at 0Example: Length is 11. Index values are 0-10 helloworld 012345678910
14
String Indexing A string of length n has characters with index values of 0 to n-1 A string of length n has characters with index values of 0 to n-1 Syntax: Syntax:<stringName>[index]
15
Indexing: Negative Offsets Negative offset: count backwards from end of string Negative offset: count backwards from end of string >>> myString = “help” >>> myString[0] ‘h’ >>> myString[-1] #first character from end ‘p’ >>> myString[-2] # second character from end ‘l’
16
String Indexing Example:myString=“hello” hello 01234
17
Strings are immutable---you cannot change them. Strings are immutable---you cannot change them. This will NOT work: myString=“help” myString=“help” myString[0]=“k” myString[0]=“k”
18
Question: String Indexing In the string, “Monday”, what is the index of ‘d’? In the string, “Monday”, what is the index of ‘d’? A. 3 B. 4 C. 5
19
Strings and for Loops Once we can index into a string, we can use a for loop to iterate through each character: Once we can index into a string, we can use a for loop to iterate through each character:myString=“hello” for i in range(len(myString)) print myString[i] print myString[i]
20
Strings and for Loops: Exercise Print the characters in a string in reverse Print the characters in a string in reverse
21
Iterating Over a String: Take Two Again, strings are sequences of characters Again, strings are sequences of characters So we can iterate… So we can iterate…Example:myString=“hello” for ch in myString print ch Output:hello hello
22
String Examples >>> myString = “Help, “ >>> myString += “I got a spam burger for dinner.” >>> print myString Help, I got a spam burger for dinner. What is the output? >>> print “-” * 80 >>> myString = “hi world hello” >>> print “hello” in myString True
23
String Examples >>> myString = “411” >>> for c in myString: print c print c411
24
Using eval(): Examples >>> numString = “400” >>> eval(numString) 400 >>> numString = “3+5” >>> eval(numString) 8
25
Exercises Write a function that takes a string argument, and prints the first character in the string (assume the string has at least one character). Write a function that takes a string argument, and prints the first character in the string (assume the string has at least one character). Write a function that takes a string argument, and prints every other character, starting with the first. Write a function that takes a string argument, and prints every other character, starting with the first.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.