1 / 25

Strings

Strings. Strings. A string is a sequence of characters. A string can have 0 characters (“”) or it can be as big as the memory on the computer running your program. Character Sets. There are 2 basic sets of characters in the computer world ASCII 8 bits size of set is 2 ** 8 big enough

connor
Download Presentation

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

  2. Strings • A string is a sequence of characters. • A string can have 0 characters (“”) or it can be as big as the memory on the computer running your program.

  3. Character Sets • There are 2 basic sets of characters in the computer world • ASCII • 8 bits • size of set is 2 ** 8 • big enough • Unicode • 32 bits • Size of set is 2 ** 32 • big!!!!!

  4. ASCII character set • http://www.asciitable.com/ • Unicode is pretty complex. • don't go there until you have to.

  5. Assigning Strings to Variables • Make a variable hold a string by enclosing the characters to be assigned in double or single quotes. e.g. userName = “billy” • Note that: aNumber = “129” • Is not the same as aNumber = 129

  6. Converting Strings • Convert a string to an int aNumber = int(aString) or aNumber = int(“562”) • Convert a int to a string aString = str(aNumber) • There's a float()version of int(), too

  7. Indexing Strings • You can pick out individual characters from a string. • >>> someStr = "Billy" • >>> print someStr[0] • B • >>> print someStr[1] • i • >>> print someStr[2] • l • >>> print someStr[3] • l • >>> print someStr[4] • y • >>> print someStr[5] • Traceback (most recent call last): • File "<pyshell#30>", line 1, in <module> • print someStr[5] • IndexError: string index out of range • >>>

  8. Indexing Strings • String indexes start at 0 (not at 1) • If you try to access a string at an index that is too high, you generate an IndexError.

  9. String Formatting • Format Specifiers: special character sequences inside strings that are meant to be replaced by other strings or floats or integers • fName = “Billy” • print “Hello %s” % fName • >>> Hello Billy

  10. String Formatting (cont’d) This thing is called a tuple • lName = Billson • print “Hello %s %s” % (fName, lName) • >>> Hello Billy Billson • The format specifiers are like placeholders which are replaced by the variables inside the brackets

  11. String Formatting (cont’d) • The order of the variables in the tuple controls the order in which they appear in the string • print “Hello %s %s” % (lName, fName) • >>> Hello Billson Billy

  12. String Formatting (cont’d) • The items in the tuple don’t have to be variables • print “Hello %s %s” % (“Mr.”, “Mac”) • >>> Hello Mr. Mac • …but they almost always are…

  13. String Formatting (cont’d) • There are format specifiers for integers as well. • age = 1052 • print “%s %s is %d years old” % \ (“Mr.”, “Mac”, age) • >>> Mr. Mac is 1052 years old • For integers we use %d

  14. String Formatting (cont’d) • There are format specifiers for float/decimal values too • discount = 1052.97 * .094538 • print “Discount: $%f” % discount • >>> Discount: $99.545678 • Since this represents money, 2 decimal places would be better: • print “Discount: $%.2f % discount • >>> Discount $99.55

  15. String Formatting (cont’d) • We can also add padding to a string • >>>print "X%20sX" % fName • X billyX • Python adds enough spaces to make fName be twenty chars wide • fName is right-justified in this example. Want left-justified? Use a ‘-’ sign. • >>> print “X%-20sX” % fName • Xbilly X

  16. String Methods • The following methods can be used on any string. Assume we have a string called s. • s.center(n) • produces a new string with s centered in a field of width n. The field is padded with spaces if n is greater than the length of s. • >>> knight = "Nee“ • >>> print "X%sX" % knight.center(10) • X Nee X

  17. Stripping White Space • s.strip() returns s with any leading or trailing whitespace (tabs, newlines, or spaces) removed • s.lstrip() – as above but only the leading whitespace is removed • s.rstrip() – as above but only the trailing whitespace is removed

  18. strip() examples • >>> knight = " Nee " • >>> print "X%sX" % knight.strip() • XNeeX • >>> print "X%sX" % knight.lstrip() • XNee X • >>> print "X%sX" % knight.rstrip() • X NeeX • >>>

  19. Lower & Upper Case • s.lower() and s.upper() convert s to lower and upper case, respectively • >>> print name.upper() • BILLY • >>> print name.lower() • billy • >>>

  20. Ex2.6 - alignIt • Ask the user for 5 first and last names. • Use string formatting to print out the first and last names (one per line) so that the last names are all lined up (table format) underneath each other. • Billy Billson • Margaret Wilson • Dmitry Dillson • Kay Killson • Yolanda Yillson

  21. Ex 2.7 boxIt.py • Pgm asks user to put in 5 strings • Pgm converts strings to upper case • Pgm strips any leading or trailing whitespace • Pgm then prints all 5 strings left justifed in a box of asterisks. • Box must have a row of asterisks at the top and at the bottom.

  22. Ex 2.4a boxIt.py (advanced)IGNORE this PLS • Talk to me for some extras needed for this exercise. (It’s complicated!) • User types in 5 strings • Pgm loops until user enters 5 unique strings • Unique means regardless of whitespace or capitalization • Pgm then prints all 5 strings left justifed in a box of asterisks. • Box must have a row of asterisks at the top and at the bottom.

  23. Escape sequences • Putting a backslash “\” before some characters gives them special meanings. • “\n” - means print a new line • “\t” - means print a tab >>> print "Billy is a\nbad,bad\t\t\tboy" • Billy is a • bad,bad boy • >>>

  24. More escape sequences • If “\” has a special meaning when it’s in front of an “n” or a “t” (& other things), how do we actually print a “\”? • >>> print "Billy is a \\nasty boy" • Billy is a \nasty boy • >>>

  25. More escape sequences • Use \’ to print a single quote • Use \” to print a double quote • >>> print "Billy\'s a \"rude\" boy" • Billy's a "rude" boy • >>>

More Related