82 Also functions like len work with strings just like the way they did with lists |
82 Also functions like len work with strings just like the way they did with lists |
83 |
83 |
84 Now lets try changing a character in the string in the same way we change lists . |
84 Now lets try changing a character in the string in the same way we change lists . |
85 |
85 |
86 type : |
86 type : |
87 w[0]='Capital H' |
87 w[0]='H' |
88 |
88 |
89 oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists . |
89 oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists . |
90 |
90 |
|
91 Now lets see some of the ways in which you can modify strings and other methods related to strings . |
|
92 |
|
93 Type : |
|
94 |
|
95 a = 'Hello world' |
|
96 |
|
97 To check if a particular string starts with a particular substring you can check that with startswith method |
|
98 |
|
99 a.startswith('Hell') |
|
100 |
|
101 Depending on whether the string starts with that substring the function returns true or false |
|
102 |
|
103 same is the case a.endwith('ld') |
|
104 |
|
105 a.upper() |
|
106 returns another string that is all the letters of given string capitalized |
|
107 |
|
108 similarly a.lower returns all small letters . |
|
109 |
|
110 Earlier we showed you how to see documentations of functions . You can see the documentation of the lower function by doing a.lower? |
|
111 |
|
112 You can use a.join to joing a list of strings to one string using a given string as connector . |
|
113 |
|
114 for example |
|
115 |
|
116 type : |
|
117 ', '.join(['a','b','c']) |
|
118 |
|
119 In this case strings are joined over , and space |
|
120 |
|
121 Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. %d can be used for formatting things like integers and %f for floats |
|
122 |
|
123 |
|
124 Their are many other string formatting options you can look at http://docs.python.org/library/stdtypes.html for more information on other options available for string formatting. |
|
125 |
|
126 |
|
127 Operators ---- Probably can be a different chapter . |
|
128 |
|
129 We will start the discussion on operators first with arithmetic operators . |
|
130 |
|
131 % can be used for remainder for example |
|
132 |
|
133 864675 % 10 gives remainder 5 |
|
134 |
|
135 |
|
136 you can use 2 *'s for power operation |
|
137 |
|
138 for example 4 ** 3 gives the result 64 |
|
139 |
|
140 One thing one should notice is the type of result depends on the types of input for example : |
|
141 |
|
142 17 / 2 both the values being integer gives the integer result 2 |
|
143 |
|
144 however the result when one or two of the operators are float is float for example: |
|
145 |
|
146 17/2.0 |
|
147 8.5 |
|
148 17.0/2.0 |
|
149 8.5 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |