|
| 1 | +>And since they did not see fit to acknowledge God, God gave them up to a debased mind and things that should no be done. They were filled with every kind of wickedness, evil, covetousness, malice. Full of envy, murder, strife, deceit, craftiness, they are gossips, slanderers, God-haters, insolent, haughty, boastful, inventors of evil, rebellious toward parents, foolish,faithless, heartless, ruthless. They know God's decree, that those who practice such things deserve to die--yet they not only do them but even applaud others who practice them. (ROMANS 1:28-32) |
| 2 | +
|
1 | 3 | #玩转字符串(1) |
2 | 4 |
|
3 | 5 | 如果对自然语言分类,有很多中分法,比如英语、法语、汉语等,这种分法是最常见的。在语言学里面,也有对语言的分类方法,比如什么什么语系之类的。我这里提出一种分法,这种分法尚未得到广大人民群众和研究者的广泛认同,但是,我相信那句“真理是掌握在少数人的手里”,至少在这里可以用来给自己壮壮胆。 |
|
6 | 8 |
|
7 | 9 | 把我的这种分法抽象一下: |
8 | 10 |
|
9 | | -- 一种是:△ + □ = ○ |
10 | | -- 另外一种是:△ + □ = △ □ |
| 11 | +- 一种是:△ +□ = ○ |
| 12 | +- 另外一种是:△ +□ = △ □ |
11 | 13 |
|
12 | 14 | 我们的语言中,离不开以上两类,不是第一类就是第二类。 |
13 | 15 |
|
|
103 | 105 |
|
104 | 106 | ###连接字符串 |
105 | 107 |
|
106 | | -- 方法1: |
107 | | - |
108 | 108 | 在IDLE中按照下面方法操作 |
109 | 109 |
|
110 | 110 | >>> a = "老齐" |
|
117 | 117 |
|
118 | 118 | 这是一种最简单连接两个字符串的方法。注意上面例子的最后一行,怎么出现乱码了?那不是乱码,是字符编码的问题。这个你权当没看见好了。不过的确是看见了。请看官google字符编码就知道了。这里推荐一篇非常好的文章:[字符集和字符编码](http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html) |
119 | 119 |
|
120 | | ->>老齐提示:看官做为学习者,一定要对所学的对象有一种好奇心,比如上面例子中,如果你满足于print c,发现结果跟自己所预料一样,这还远远不够。如果你向下走了一行,就发现一个怪怪的结果了,这就让你在编程路上又前进一大步。所以,要有对世界好奇的心,不断探索、思考和尝试。反正在计算机上尝试,也没有多大成本。最坏的结果是关掉IDLE罢了。 |
| 120 | +>提示:看官做为学习者,一定要对所学的对象有一种好奇心,比如上面例子中,如果你满足于print c,发现结果跟自己所预料一样,这还远远不够。如果你向下走了一行,就发现一个怪怪的结果了,这就让你在编程路上又前进一大步。所以,要有对世界好奇的心,不断探索、思考和尝试。反正在计算机上尝试,也没有多大成本。最坏的结果是关掉IDLE罢了。 |
| 121 | +
|
| 122 | +用`+`号实现连接,的确比较简单,不过,有时候你会遇到这样的问题: |
| 123 | + |
| 124 | + >>> a = 1989 |
| 125 | + >>> b = "free" |
| 126 | + >>> print b+a |
| 127 | + Traceback (most recent call last): |
| 128 | + File "<stdin>", line 1, in <module> |
| 129 | + TypeError: cannot concatenate 'str' and 'int' objects |
| 130 | + |
| 131 | +抱错了,其错误原因已经打印出来了(一定要注意看打印出来的信息):`cannot concatenate 'str' and 'int' objects`。原来`a`对应的对象是一个`int`类型的,不能将它和`str`对象连接起来。怎么办? |
| 132 | + |
| 133 | +可以用下面三种方法中的任何一种: |
121 | 134 |
|
122 | | -对字符串的简单操作暂且到这里,下一篇文章继续,还没完,请明天再来。 |
| 135 | + >>> print b + `a` #注意,` `是反引号,不是单引号,就是键盘中通常在数字1左边的那个,在英文半角状态下输入的符号 |
| 136 | + free1989 |
| 137 | + >>> print b + str(a) #str(a)实现将整数对象转换为字符串对象 |
| 138 | + free1989 |
| 139 | + >>> print b + repr(a) #repr(a)与上面的类似 |
| 140 | + free1989 |
| 141 | + |
| 142 | +可能看官看到这个,就要问它们三者之间的区别了。首先明确,repr()和``是一致的,就不用区别了。接下来需要区别的就是repr()和str,一个最简单的区别,repr是函数,str是跟int一样,一种对象类型。不过这么说是不能完全解惑的。幸亏有那好的google让我辈使用,你会找到不少人对这两者进行区分的内容,我推荐这个: |
| 143 | + |
| 144 | +>1. When should i use str() and when should i use repr() ? |
| 145 | +> |
| 146 | +>Almost always use str when creating output for end users. |
| 147 | +> |
| 148 | +>repr is mainly useful for debugging and exploring. For example, if you suspect a string has non printing characters in it, or a float has a small rounding error, repr will show you; str may not. |
| 149 | +> |
| 150 | +>repr can also be useful for for generating literals to paste into your source code. It can also be used for persistence (with ast.literal_eval or eval), but this is rarely a good idea--if you want editable persisted values, something like JSON or YAML is much better, and if you don't plan to edit them, use pickle. |
| 151 | +
|
| 152 | +>2.In which cases i can use either of them ? |
| 153 | +> |
| 154 | +>Well, you can use them almost anywhere. You shouldn't generally use them except as described above. |
| 155 | +
|
| 156 | +>3.What can str() do which repr() can't ? |
| 157 | +> |
| 158 | +>Give you output fit for end-user consumption--not always (e.g., str(['spam', 'eggs']) isn't likely to be anything you want to put in a GUI), but more often than repr. |
| 159 | +> |
| 160 | +>4.What can repr() do which str() can't |
| 161 | +> |
| 162 | +>Give you output that's useful for debugging--again, not always (the default for instances of user-created classes is rarely helpful), but whenever possible. |
| 163 | +> |
| 164 | +>And sometimes give you output that's a valid Python literal or other expression--but you rarely want to rely on that except for interactive exploration. |
| 165 | +
|
| 166 | +以上英文内容来源:http://stackoverflow.com/questions/19331404/str-vs-repr-functions-in-python-2-7-5 |
123 | 167 |
|
124 | 168 | ##Python转义字符 |
125 | 169 |
|
|
154 | 198 | you can connect me by qq\weibo\gmail |
155 | 199 |
|
156 | 200 | 看官自己试试吧。如果有问题,可以联系我解答。 |
157 | | - |
158 | | - |
|
0 commit comments