You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/05-Strings.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,13 @@
2
2
3
3
<h2>Introduction</h2>
4
4
5
-
>Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). Python strings are "immutable" which means they cannot be changed after they are created. Since strings can't be changed, we construct *new* strings as we go to represent computed values. So, for example the expression ('hello' + 'there') takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'.</br>
5
+
>Python has a built-in string class named "str" with many handy features (there is an older module named "string" which you should not use). Python strings are "immutable" which means they cannot be changed after they are created. Since strings can't be changed, we construct *new* strings as we go to represent computed values.
6
6
7
-
>Characters in a string can be accessed using the standard [] syntax. Python uses zero-based indexing, so if str is 'hello' str[1] is 'e'. If the index is out of bounds for the string, Python raises an error. The Python style (unlike Perl) is to halt if it can't tell what to do, rather than just make up a default value. The handy "slice" syntax (below) also works to extract any substring from a string. The len(string) function returns the length of a string. The [] syntax and the len() function actually work on any sequence type -- strings, lists, etc. Python tries to make its operations work consistently across different types. The '+' operator can concatenate two strings.</br>
7
+
>So, for example the expression ('hello' + 'there') takes in the 2 strings 'hello' and 'there' and builds a new string 'hellothere'.
8
8
9
-
>Notice in the code below that variables are not pre-declared -- just assign to them and go.</br>
9
+
>Characters in a string can be accessed using the standard [] syntax. Python uses zero-based indexing, so if str is 'hello' str[1] is 'e'. If the index is out of bounds for the string, Python raises an error. The handy "slice" syntax (below) also works to extract any substring from a string. The len(string) function returns the length of a string. The [] syntax and the len() function actually work on any sequence type -- strings, lists, etc. Python tries to make its operations work consistently across different types. The '+' operator can concatenate two strings.
10
+
11
+
>Notice in the code below that variables are not pre-declared -- just assign to them and go.
10
12
11
13
_**Example**_
12
14
```python
@@ -16,7 +18,7 @@ print len(s) ## 2
16
18
print s +' there'## hi there
17
19
```
18
20
19
-
>The str() function converts values to a string form so they can be combined with other strings.</br>
21
+
>The str() function converts values to a string form so they can be combined with other strings.
20
22
21
23
_**Example**_
22
24
```python
@@ -25,11 +27,9 @@ pi = 3.14
25
27
text ='The value of pi is '+str(pi) ## yes
26
28
```
27
29
28
-
>For numbers, the standard operators, +, /, * work in the usual way. There is no ++ operator, but +=, -=, etc. work. If you want integer division, it is most correct to use 2 slashes -- e.g. 6 // 5 is 1 (previous to python 3000, a single / does int division with ints anyway, but moving forward // is the preferred way to indicate that you want int division.)</br>
29
-
30
30
<h2>String Methods</h2>
31
31
32
-
>Here are some of the most common string methods. A method is like a function, but it runs "on" an object.</br>
32
+
>Here are some of the most common string methods. A method is like a function, but it runs "on" an object.
33
33
34
34
<h2>Functions</h2>
35
35
@@ -48,7 +48,7 @@ text = 'The value of pi is ' + str(pi) ## yes
48
48
49
49
<h2>String Slices</h2>
50
50
51
-
>The "slice" syntax is a handy way to refer to sub-parts of sequences -- typically strings and lists. The slice s[start:end] is the elements beginning at start and extending up to but not including end. Suppose we have s = "Hello"</br>
51
+
>The "slice" syntax is a handy way to refer to sub-parts of sequences -- typically strings and lists. The slice s[start:end] is the elements beginning at start and extending up to but not including end. Suppose we have s = "Hello"
52
52
53
53
| <center>Function </center> | <center>What it does</center>
54
54
| :------------- | :-------------
@@ -59,7 +59,7 @@ text = 'The value of pi is ' + str(pi) ## yes
59
59
60
60
61
61
62
-
>The standard zero-based index numbers give easy access to chars near the start of the string. As an alternative, Python uses negative numbers to give easy access to the chars at the end of the string: s[-1] is the last char 'o', s[-2] is 'l' the next-to-last char, and so on. Negative index numbers count back from the end of the string:</br>
62
+
>The standard zero-based index numbers give easy access to chars near the start of the string. As an alternative, Python uses negative numbers to give easy access to the chars at the end of the string: s[-1] is the last char 'o', s[-2] is 'l' the next-to-last char, and so on. Negative index numbers count back from the end of the string:
63
63
64
64
| <center>Function </center> | <center>What it does</center>
65
65
| :------------- | :-------------
@@ -72,15 +72,15 @@ text = 'The value of pi is ' + str(pi) ## yes
72
72
73
73
<h2>String %</h2>
74
74
75
-
>Python has a printf()-like facility to put together a string. The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point), and the matching values in a tuple on the right (a tuple is made of values separated by commas, typically grouped inside parentheses):</br>
75
+
>Python has a printf()-like facility to put together a string. The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point), and the matching values in a tuple on the right (a tuple is made of values separated by commas, typically grouped inside parentheses):
76
76
77
77
_**Example**_
78
78
```python
79
79
## % operator
80
80
text ="%d little pigs come out or I'll %s and %s and %s"% (3, 'huff', 'puff', 'blow down')
81
81
```
82
82
83
-
>The above line is kind of long -- suppose you want to break it into separate lines. You cannot just split the line after the '%' as you might in other languages, since by default Python treats each line as a separate statement (on the plus side, this is why we don't need to type semi-colons on each line). To fix this, enclose the whole expression in an outer set of parenthesis -- then the expression is allowed to span multiple lines. This code-across-lines technique works with the various grouping constructs detailed below: ( ), [], { }.</br>
83
+
>The above line is kind of long -- suppose you want to break it into separate lines. You cannot just split the line after the '%' as you might in other languages, since by default Python treats each line as a separate statement (on the plus side, this is why we don't need to type semi-colons on each line). To fix this, enclose the whole expression in an outer set of parenthesis -- then the expression is allowed to span multiple lines. This code-across-lines technique works with the various grouping constructs detailed below: ( ), [], { }.
0 commit comments