Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
s[i] Introduction to Computer Programming Strings CSCI-UA 2 - - PowerPoint PPT Presentation
s[i] Introduction to Computer Programming Strings CSCI-UA 2 - - PowerPoint PPT Presentation
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 Pythons primary Strings data types Strings can be used
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 CSCI-UA 2
Characters
- rd('a')
Strings Strings and Characters
All characters have a corresponding character code 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
Decimal Hex Char Decimal Hex Char Decimal Hex Char Decim1 al Hex Char
(NULLJ32 20
(SPACE} 6440
@)96
60
'1
(STAR · T OF HE DING]33 21 65 41
A97
6 2
{START OF TEXT]34 22
6642
98 62b
3 {END OF T E XT]35
2367
3 C , 9963
C 4f
END OF TRANSMISSION 3624 68
4 D100
64d
5 {E NQUIRY/37 25
%69 45 E 101 65
e 6 [AC(NOWLEDGEJ38 26
&70 46
F102 66 7
(BELtJ391 27 71
47103 67
g8
(BACKS~ CEJ40 28 72 48 104
68h
9 ORI41
2973 49
105
610
A {UNEFEED]42
2A74
AJ
106 6A
j11
B [V E RTICAL TAB] 43 28+
75 4B107
6Bk 12
C {F ORM FE EDJ 442C
,
764C
L108 6C
I
13
D [CAaRJAGE RETURN] 4S20 77 40
M109
6Dm
14
E [SHIFTQ46
2 78 4E110
6En
15
F (SHIFTINJ47 2 I 79 4 111
6F16 10
{D TA LINK ESCAPE] 4830
80
50 p
112 70
p,17
11 {DEVICE CONTROL l]49 31 81
51 Q
113
71 q 18 12
[DEVICE CONTROL 2]50 32
82
52114 72
r
19 13
f
DEVICE CONTROL 3}51 33
83 53s
115 73
s20
14
[DEVICE CONTROL 4]52
384
54 T116
7421
15
{NEGATIVE AG 'NOWLEDGEJ 5335 85
55
u
117
75
u
22 16
(SYNCHRONOUS IDLE} 54 3686
56
V118 76
V23 17
(ENGOF
TRANS. 81.0CKJ55
3787
57
119 77
w
24 18
CANCEL56
38 88 58
120 78
X 2519
[E ND OF M E DIUM}57 39
91 89
59 12179 y 26 lA
{SU B STITUT E ]58
3A- 90
z
122 7A z
- 27
lB
[E SCAl'EJ59
3B
- 91
123
7B {'
28
lC
{FILE SEPARATOR] 60 3C< 92
SC \124 7C 29
D[GROUP S 'ARATORl 61
3D- 93
50 125 70
- 30
lE
(RECORD SEPARATOR]62
3E > 94
5
126 7E 31 lf
{UNIT SEPARATOR}63 3f
? 95 SF127
7F (DEL]Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Indexing s[i]
Strings are defined as ordered collections of characters 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
1 2 3 4 5 p y t h
- n
- 6
- 5
- 4
- 3
- 2
- 1
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Slicing Strings s[begin:end]
Slicing is for extracting substrings from a string 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
- f the string minus one
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Testing methods
String Methods
Searching methods
s.method()
Formatting methods Stripping methods Splitting methods Replacement methods
Introduction to Computer Programming Strings CSCI-UA 2 Strings and Characters
Escape Characters \
“Escape characters” are a special 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