forked from CalebCurry/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-strings.py
More file actions
217 lines (158 loc) · 6.36 KB
/
02-strings.py
File metadata and controls
217 lines (158 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
me = "Caleb"
#escape character example
print("\n")
me = "C\ta\tl\te\tb\n"
print(me)
#can also use single quotes
you = 'Subscriber'
me = "Caleb" #reset to normal
#passing multiple arguments to print
print(me, you) #output: Caleb Subscriber
#double quotes and single quotes work the same way with the exception of working with quotes inside.
#here are some examples:
single_quotes = 'She said "Hi"'
print(single_quotes) #output: She said "Hi"
double_quotes = "She said \"Hi\""
print(double_quotes) #output: She said "Hi"
single_quotes = 'I\'m learning!'
print(single_quotes) #output: I'm learning!
#notice we have to escape the same quote as the surrounding quote
#this is the same as:
#single_quotes = 'I\'m learning!' #output: I'm learning
double_quotes = "I'm learning!"
print(double_quotes)
#notice we have to escape the same quote as the surrounding quote
#Here are the other escape sequences:
#https://docs.python.org/2.0/ref/strings.html
#Notice that if you want to print \ you must put two
print("\\") #output: \
#This is because \ is an escape character.
#If you want to print a single quote, you can use double quotes to surround it
#or escape it with a backslash
#If you want to print a double quote, you can use single quotes to surround it
#you can also prefix with r which will make a raw string (ignoring escapes except same quote)
print(r'as raw as I\'ve ever seen. \/\/ () \/\/. \t' ) #only \' is escaped
#output: as raw as I've ever seen. \/\/ () \/\/. \t
#This is useful for regex and file paths.
#https://docs.python.org/3/reference/lexical_analysis.html#string-literal
#https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
########## CONCATENTATION ##########
#Use a + to concatenate
msg = me + " + " + you
print(msg)
#Can use comma separated values in print. Automatically uses spaces between
print(me, "+", you)
#You can automatically concatenate literals (actual values with quotes as opposed to variables)
#by putting them one after the other. This is ideal if you need to split a large string up
#onto multiple lines.
print("my " "name "
"is " "Caleb")
#You can also use multiline string
print("""Name: Caleb
Age: 58""")
#skip newline using \ (without it, it would go down a line each line)
print("""\
Name: Caleb. \
Age: 58""")
"""You may see
them as multi-
line comments even
if they technically
are not.
https://stackoverflow.com/questions/7696924/is-there-a-way-to-create-multiline-comments-in-python
"""
########## INDEXES ##########
#It's very common to grab particular characters within a string
#this is also common for collections when we get to em.
msg = "This is a very important message."
print(msg[5])
#indexing is zero based. This means 0 will get the very first character:
print(msg[0])
#This value is returned and can be assigned to a variable or used in an expression
first_letter = msg[0]
print(first_letter + "acos")
#You can also index from the right.
period = msg[32] #from left
print(period)
period = msg[-1] #from right
print(period)
#This may be obvious, but we do not use -0 to get the first element from the right as we would
#use 0 to get the first element from the left. (Side note) -0 actually is 0:
print(-0) #(side note)
print(0 == -0) #0 == 0 (side note)
########## SLICING #########
#repeating this for ease of reading:
msg = "This is a very important message."
#We can pass two numbers in for the index to get a series of characters.
#left is included. Right is excluded.
#this will get 2 characters (from index 1 - 3 with 3 not included...we are left with index 1 and 2.
print(msg[1:3]) #hi
#You can also leave off first to start at beginning
#or leave off second to go to end
print(msg[:5]) #print index 0-4 (because 5 is excluded, remember)
print (msg[1:]) #from index 1 to end
#We can also use negatives. Here is how you get the last 8 characters:
print(msg[-8:]) #start 8 from right and go to end
#out of range index
#Grabbing an out of range index directly will cause an error.
#But incorrect ranges fail gracefully.
#print(msg[42]) #doesn't work
print(msg[42:43]) #works
########## IMMUTABILITY ##########
#Strings are immutable, meaning they can't change.
cant_change = "Java is my favorite!"
#cant_change[0] = K .....nope. Sorry Kava (my dog)
#generate new string from old:
#Kava is my favorite!
new = 'K' + cant_change[1:]
print(new)
#Python is my favorite!
fav_language = "Python " + cant_change[5:]
print(fav_language) #output: Python is my favorite!
#Java is actually coffee
coffee = cant_change[:8] + "actually coffee" #grab first 7 characters (index 8 not included)
print(coffee) #output: Java is actually coffee
#operations that appear to change string actually replace:
#Java is actually coffee (contrary to popular belief).
coffee += " (contrary to popular belief)."
print(coffee) #output: Java is actually coffee (contrary to popular belief).
########## GETTING STRING LENGTH ##########
#There is a function we can use....
#similar to how we invoke print to do something for us (output to console)
#we can invoke len to count for us:
print(len(coffee)) #output: 43
#for those from other languages...
#notice we do not say coffee.len()
#coffee.len() XXXXXX
#nope.
#last index is always len() - 1.
name = "Caleb"
print("index 4:", name[4]) #b
print("len(name)", len(name)) #length is 5
########## MORE STRING WORK ##########
#How to convert a number to a string
length = len(name)
print("Length is " + str(length)) #output: Length is 5
#this works however sometimes you just need one combined string instead of components.
#when we use a comma, we are passing in data as separate arguments.
#fortunately, print knows how to handle it. Other times, we must pass in one string.
#WARNING --> Commas automatically print a space.
print("length is ", length)
#an example of this is if we need a variable. We cannot use a comma:
#BAD
msg = "length is", len(name)
print(msg) #NOT WHAT WE WANTED!
#output: length is 5 ^ not what we wanted do below instead
#GOOD
length = len(name)
msg = "length is " + str(length)
print(msg) #output: length is 5
#EVEN BETTER
#We can also nest function calls:
print("length is " + str(len(name)))
#The order in which these are invoked are in to out...
#len(name) is first which returns a number.
#this number is passed to str which converts it to a string
#this string is then concatenated with the string on the left
#this final string is then passed to print
#That's the end of your introduction to strings!