Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters s[i]
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters Strings are one of Python’s primary Strings data types Strings can be used to represent anything that can be encoded as text Examples: symbols, words, text files, Python programs, and Web pages We’ve already been using strings quite a bit and will now focus on accessing and processing them
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters All characters have a corresponding Characters character code ord('a') Python uses Unicode to assign character codes Unicode is a common standard for encoding thousands of symbols and characters from many languages The ord() function can be used to get the Unicode number for any character The chr() function can be used to get the character for any Unicode number
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters Decimal Hex Char Decimal Hex Char Decimal Hex Char Decim1 al Hex Char 40 (NULLJ 32 20 (SPACE} 64 60 ' 0 @) 96 1 HE 33 21 65 41 6 (STAR · T OF DING] A 97 2 {START OF TEXT] 34 22 42 66 98 62 b , 99 63 3 {END OF T E XT] 35 67 3 C 23 C f TRANSMISSION 36 24 68 100 4 END OF 4 D 64 d 37 25 45 101 65 5 {E NQUIRY/ % 69 E e 6 [AC(NOWLEDGEJ 26 70 46 F 102 38 & 66 39 1 (BELtJ 27 71 103 67 7 47 g 8 40 28 72 48 104 (BACKS~ CEJ 68 h 0 41 73 49 105 9 ORI 29 6 10 74 J 106 6A {UNEFEED] 42 A 2A A j + 11 107 B [V E RTICAL TAB] 43 28 75 4B 6B k , 12 2C 4C 108 6C {F ORM FE EDJ 44 76 L I C 40 13 D [CAaRJAGE RETURN] 4S 20 77 M 109 6D m 46 14 110 E [SHIFTQ 2 78 4E 6E n 15 47 2 79 4 111 F (SHIFTINJ I 0 6F p p, 16 10 {D TA LINK ESCAPE] 30 80 112 70 48 0 50 17 49 31 81 113 {DEVICE CONTROL l] 51 Q 71 q 11 50 32 18 12 82 114 72 r [DEVICE CONTROL 2] 52 f s 51 19 13 33 115 73 DEVICE CONTROL 3} 83 53 s 14 20 [DEVICE CONTROL 4] 52 3 84 54 T 116 74 u 15 21 35 55 117 75 {NEGATIVE AG 'NOWLEDGEJ 53 85 u 22 16 118 76 (SYNCHRONOUS IDLE} 54 36 86 56 V V 23 17 (ENG OF TRANS. 81.0CKJ 37 87 119 77 55 57 w 56 24 120 18 CANCEL 38 88 58 78 X 91 57 39 19 89 79 y 25 [E ND OF M E DIUM} 59 121 z 26 lA 3A • 122 7A {SU B STITUT E ] 58 90 5A z • 3B • lB 59 91 [ 123 27 [E SCAl'EJ 5B 7B { ' 28 lC SEPARATOR] 92 124 7C {FILE 60 3C < SC \ - - 29 61 93 D [GROUP S 'ARATORl 3D 50 125 70 lE 3E 30 (RECORD SEPARATOR] 62 94 126 7E > 5 31 lf 63 3f 127 {UNIT SEPARATOR} ? 95 SF 7F (DEL]
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters Strings are defined as ordered Indexing collections of characters s[i] Therefore, we can access these characters by position with “indexing” Offsets start at 0 and end at one less than the length of the string Offset can also be specified negatively
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters 0 1 2 3 4 5 p y t h o n -6 -5 -4 -3 -2 -1
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters Slicing is for extracting substrings from Slicing Strings a string s[begin:end] To slice a string, indicate the first character you want and one past the last character First index location is always 0 Last index location is always the length of the string minus one
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters Testing methods String Methods s.method() Searching methods Formatting methods Stripping methods Splitting methods Replacement methods
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters “Escape characters” are a special Escape Characters notation for whitespace and other \ unprintable characters Used for characters without a standard symbol or symbols Python sets aside Escape characters begin with a backslash Escape sequences are considered one character
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Recommend
More recommend