22
33This section covers the basics of text manipulation.
44
5- ## Reading
6-
75### Representing Text
86
97String are text literals written in programs with quotes.
@@ -28,6 +26,9 @@ Triple quotes capture all text enclosed in multiple lines.
2826
2927### String escape codes
3028
29+ Escape codes are used to represent control characters and characters that can't be easily typed
30+ at the keyboard. Here are some common escape codes:
31+
3132```
3233'\n' Line feed
3334'\r' Carriage return
@@ -36,7 +37,6 @@ Triple quotes capture all text enclosed in multiple lines.
3637'\"' Literal double quote
3738'\\'` Literal backslash
3839```
39- These codes are inspired by C.
4040
4141### String Representation
4242
@@ -45,9 +45,9 @@ Each character represents a raw unicode code point.
4545<!-- TODO: Add Picture of following characters -->
4646
4747``` python
48- a = ' \xf1 ' # a = 'ñ'
49- b = ' \u2200 ' # b = '∀'
50- c = ' \U0001D122 ' # c = '𝄢'
48+ a = ' \xf1 ' # a = 'ñ'
49+ b = ' \u2200 ' # b = '∀'
50+ c = ' \U0001D122 ' # c = '𝄢'
5151d = ' \N{FORALL} ' # d = '∀'
5252```
5353
@@ -114,11 +114,13 @@ Replacing text.
114114
115115``` python
116116s = ' Hello world'
117- t = s.replace(' Hello' , ' Hallo' )
117+ t = s.replace(' Hello' , ' Hallo' ) # 'Hallo world'
118118```
119119
120120** More string methods:**
121121
122+ Strings have a wide variety of other methods for testing and manipulating the text data:
123+
122124``` python
123125s.endswith(suffix) # Check if string ends with suffix
124126s.find(t) # First occurrence of t in s
@@ -136,11 +138,11 @@ s.split([delim]) # Split string into list of substrings
136138s.startswith(prefix) # Check if string starts with prefix
137139s.strip() # Strip leading/trailing space
138140s.upper() # Convert to upper case
141+ ```
139142
140143### String Mutability
141144
142- Strings are " immutable" . They are read only.
143-
145+ Strings are "immutable" or read-only.
144146Once created, the value can't be changed.
145147
146148``` python
@@ -198,20 +200,20 @@ data = text.encode('utf-8') # text -> bytes
198200
199201### Raw Strings
200202
201- String with uninterpreted backslash.
203+ Raw strings are strings with uninterpreted backslash. They are little by prefixing the initial quote with a lowercase "r" .
202204
203205``` python
204206>> > rs = r ' c:\n ewdata\t est' # Raw (uninterpreted backslash)
205207>> > rs
206208' c:\\ newdata\\ test'
207209```
208210
209- String is the literal text, exactly as typed.
211+ The string is the literal text, exactly as typed.
210212This is useful in situations where the backslash has special significance. Example: filename, regular expressions, etc.
211213
212214### f-Strings
213215
214- String with formatted expression substitution.
216+ A string with formatted expression substitution.
215217
216218``` python
217219>> > name = ' IBM'
@@ -226,9 +228,9 @@ String with formatted expression substitution.
226228>> >
227229```
228230
229- ** Note: Requires Python 3.6 or newer.**
231+ ** Note: This requires Python 3.6 or newer.**
230232
231- ## Exercises 1.4
233+ ## Exercises
232234
233235
234236In this exercise, we experiment with operations on Python's string type.
0 commit comments