python12(): Exercises for characters, strings, printing, files, text. Indexing a string s = "A flock of seagulls" s[0:4] = "A fl" s[-1] = "s" s[6:16:2] = "ko eg" Concatenate strings s1 and s2 by s = s1 + s2. 4:00pm Wednesday noon Wednesday 10:00am Monday 10:00am Monday noon Wednesday 4:00pm Friday ord(c) gives index, chr(index) gives character ord(" ") = 32 chr( 32 ) = " " ord("0") = 48 chr( 48 ) = "0" ord("1") = 49 chr( 49 ) = "1" ord("A") = 65 chr( 65 ) = "A" ord("Z") = 90 chr( 90 ) = "Z" ord("a") = 97 chr( 97 ) = "a" Use ord() on each character in a string: ord(M) = 77 ord(o) = 111 ord(n) = 110 ord(t) = 116 ord(y) = 121 ord( ) = 32 ord(P) = 80 ord(y) = 121 ord(t) = 116 ord(h) = 104 ord(o) = 111 ord(n) = 110 str() converts an integer or real to a string "journal_1900.txt" was published in 1900 "journal_1901.txt" was published in 1901 "journal_1902.txt" was published in 1902 "journal_1903.txt" was published in 1903 "journal_1904.txt" was published in 1904 "journal_1905.txt" was published in 1905 "theta_0.0.jpg" is an image of data for theta = 0.0 "theta_0.25.jpg" is an image of data for theta = 0.25 "theta_0.5.jpg" is an image of data for theta = 0.5 "theta_0.75.jpg" is an image of data for theta = 0.75 "theta_1.0.jpg" is an image of data for theta = 1.0 str().zfill() uses leading zeros to pad the output string. file # 5 is "inflammation-05.csv" file # 6 is "inflammation-06.csv" file # 7 is "inflammation-07.csv" file # 8 is "inflammation-08.csv" file # 9 is "inflammation-09.csv" file # 10 is "inflammation-10.csv" file # 11 is "inflammation-11.csv" file # 12 is "inflammation-12.csv" file # 13 is "inflammation-13.csv" file # 14 is "inflammation-14.csv" upper() and lower() change the case of a character or string. s1 = " My name is Elmer! " s2 = s1.lower ( ) = " my name is elmer! " s3 = s1.upper ( ) = " MY NAME IS ELMER! " s4 = s1.title ( ) = " My Name Is Elmer! " .strip(), .lstrip(), .rstrip() remove all, left, or right blanks. s1 = " My name is Elmer! " s4 = s1.strip() = "My name is Elmer!" s5 = s1.rstrip() = " My name is Elmer!" s6 = s1.lstrip() = "My name is Elmer! " .replace() replaces one character with another. s1 = " My name is Elmer! " s7 = s1.replace(" ","") = "MynameisElmer!" s8 = s1.replace(" ","_") = "___My_name_is_Elmer!__" s9 = s1.replace(e,Z) = " My namZ is ElmZr! " You can't use indexing to change a string. prime = np.array ( [ 2, 3, 5, 7, 11, 15, 17 ] ) [ 2 3 5 7 11 15 17] prime[5] = 13 [ 2 3 5 7 11 13 17] city = 'Pittzburgh' Pittzburgh city[4] = 's' That did not work! Pittzburgh city2 = city[0:4] + 's' + city[5:] Pittsburgh city = 'Pittsburgh' Pittsburgh city = city + ', Pennsylvania' Pittsburgh, Pennsylvania Characters have an ordering. 'A' < 'B' = True 'A' < 'a' = True 'B' < 'a' = True '0' < 'A' = True '&' < '5' = True Strings can be ordered by character comparison ants > anthrax Printed values can include a label: The square root of 10 = 3.1622776601683795 The square root of 10 = 3.162278 The square root of 10 = 3.16 The square root of 10 = 3.1622776601683795 The square root of 10 is 3.16 to two decimal places. Printing a table neatly requires some formatting. n n^2 n^3 n^4 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256 5 25 125 625 6 36 216 1296 7 49 343 2401 8 64 512 4096 9 81 729 6561 10 100 1000 10000 n n^2 n^3 n^4 1 1 1 1 2 4 8 16 3 9 27 81 4 16 64 256 5 25 125 625 6 36 216 1296 7 49 343 2401 8 64 512 4096 9 81 729 6561 10 100 1000 10000 More about formats. I have 3 brothers and 12 cats A circle of radius 10 has area roughly 31 1 1 1 2 4 16 3 9 81 4 16 256 1 01 0001 2 04 0016 3 09 0081 4 16 0256 The value of pi is 3.141592653589793 The value of pi is 3.14159 The value of pi is 3.14 The value of pi is 3.142 The value of 5^10 is 6.250000e+02 The value of 5^10 is 625.000000 The value of 5^10 is 625 First Last name Age -------- ---------- --- Viktor Frankenstein 35 Andy Warhola 45 Engelbert Humperdinck 55 First Last name Age -------- ---------- --- Viktor Frankenstein 35 Andy Warhola 45 Engelbert Humperdinck 55 Graphics saved as "solar_data_1963.png" Several print statements can be combined on one line. The square root of 10 = 3.16 to two decimal places. \n is newline, \t is a tab. Polonius: What do you read, my lord? Hamlet: Words, words, words! Polonius: What do you read, my lord? Hamlet: Words, words, words! Characters in Hamlet: Gertrude: The queen (poisoned) Horatio: Hamlet's friend (alive at end) Ophelia: Hamlet's fiancee (drowned) Polonius: A courtier (stabbed) To actually print strings like \n and \t, use TWO backslashes (\). Similarly, print a quote inside a quoted string using a backslash: That quote is from Hamlet's meeting with Polonius.