1 / 25

Strings

Strings. CS303E: Elements of Computers and Programming. The String Data Type . Represents text So far, we’ve used it for input and output Now, we’ll take a closer look: Representation String operations Like arithmetic operations, but different. String Representation.

ashtyn
Télécharger la présentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings CS303E: Elements of Computers and Programming

  2. The String Data Type • Represents text • So far, we’ve used it for input and output • Now, we’ll take a closer look: • Representation • String operations • Like arithmetic operations, but different

  3. String Representation • Defined with single or double quotes • Need to start and end with same type of quote • firstName=‘Steve’ • lastName=“Jobs” • Represented as a sequence of characters

  4. Strings as Input • Get using raw_input() • 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

  5. String Concatenation • “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 • Math and strings • Determines whether to add or concatenate based on the first operand • <number> + … indicates math • <string> + … indicates concatenation • canNOT combine the two

  7. String Concatenation • But what if you want to combine the two? • Convert numbers to strings using str() • 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 • Concatenate a string with itself Example: print “Hello”*3 Output: HelloHelloHello • * is also overloaded

  9. String Membership • You can test to see if a string is a member of a larger string Example: myString = “Hello” print “el” in myString Output: True

  10. Question:String Concatenation • 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 • 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 • Characters are numbered, or indexed, beginning at 0 Example: Length is 11. Index values are 0-10

  14. String Indexing • A string of length n has characters with index values of 0 to n-1 • Syntax: <stringName>[index]

  15. Indexing: Negative Offsets • 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”

  17. String Indexing • Strings are immutable---you cannot change them. This will NOT work: myString=“help” myString[0]=“k”

  18. Question:String Indexing • 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: myString=“hello” for i in range(len(myString)) print myString[i]

  20. Strings and for Loops:Exercise • Print the characters in a string in reverse

  21. Iterating Over a String:Take Two • Again, strings are sequences of characters • So we can iterate… Example: myString=“hello” for ch in myString print ch Output: h e l l o

  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 4 1 1

  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 every other character, starting with the first.

More Related