<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > 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) 如果對自然語言分類,有很多中分法,比如英語、法語、漢語等,這種分法是最常見的。在語言學里面,也有對語言的分類方法,比如什么什么語系之類的。我這里提出一種分法,這種分法尚未得到廣大人民群眾和研究者的廣泛認同,但是,我相信那句“真理是掌握在少數人的手里”,至少在這里可以用來給自己壯壯膽。 我的分法:一種是語言中的兩個元素(比如兩個字)和在一起,出來一個新的元素(比如新的字);另外一種是兩個元素和在一起,知識兩個元素并列。比如“好”和“人”,兩個元素和在一起是“好人”,而3和5和在一起是8,如果你認為是35,那就屬于第二類和法了。 把我的這種分法抽象一下: - 一種是:△ +□ = ○ - 另外一種是:△ +□ = △ □ 我們的語言中,離不開以上兩類,不是第一類就是第二類。 太天才了。請鼓掌。 ## 字符串 在我洋洋自得的時候,我google了一下,才發現,自己沒那么高明,看[維基百科的字符串詞條](http://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E4%B8%B2)是這么說的: > 字符串(String),是由零個或多個字符組成的有限串行。一般記為s=a[1]a[2]...a[n]。 看到維基百科的偉大了吧,它已經把我所設想的一種情況取了一個形象的名稱,叫做字符串 根據這個定義,在前面兩次讓一個程序員感到偉大的"Hello,World",就是一個字符串。或者說不管用英文還是中文還是別的某種問,寫出來的文字都可以做為字符串對待,當然,里面的特殊符號,也是可以做為字符串的,比如空格等。 操練一下字符串吧。 ~~~ >>> print "good good study, day day up"good good study, day day up>>> print "----good---study---day----up"----good---study---day----up ~~~ 在print后面,打印的都是字符串。注意,是雙引號里面的,引號不是字符串的組成部分。它是在告訴計算機,它里面包裹著的是一個字符串。也就是在python中,通常用一對雙引號、或者單引號來包裹一個字符串。或者說,要定義一個字符串,就用雙引號或者單引號。 愛思考的看官肯定發現上面這句話有問題了。如果我要把下面這句話看做一個字符串,應該怎么做? ~~~ 小明說"我沒有燒圓明園" ~~~ 或者這句 ~~~ What's your name? ~~~ 問題非常好,有道理。在python中有一種方法專門解決類似的問題。看下面的例子: ~~~ >>> print "小明說:\"我沒有少圓明園\""小明說"我沒有少圓明園" ~~~ 這個例子中,為了打印出那句含有雙引號的字符串,也就是雙引號是字符串的一部分了,使用了一個符號:\,在python中,將這個符號叫做轉義符。本來雙引號表示包括字符串,它不是字符串一部分,但是如果前面有轉義符,那么它就失去了原來的含義,轉化為字符串的一部分,相當于一個特殊字符了。 下面用轉義符在打印第二句話: ~~~ >>> print 'what\'s your name?'what's your name? ~~~ 另外,雙引號和單引號還可以嵌套,比如下面的句子中,單引號在雙引號里面,雖然沒有在單引號前面加轉義符,但是它被認為是字符串一部分,而不是包裹字符串的符號 ~~~ >>> print "what's your name?" #雙引號包裹單引號,單引號是字符what's your name?>>> print 'what "is your" name' #單引號包裹雙引號,雙引號是字符what "is your" name ~~~ ## 變量連接到字符串 前面講過變量了,并且有一個釣魚的比喻。如果忘記了,請看前一章內容。 其實,變量不僅可以跟數字連接,還能夠跟字符串連接。 ~~~ >>> a=5>>> a5>>> print a5>>> b="hello,world">>> b'hello,world'>>> print bhello,world ~~~ 還記得我們曾經用過一個type命令嗎?現在它還有用,就是檢驗一個變量,到底跟什么類型聯系著,是字符串還是數字? ~~~ >>> type(a)<type 'int'>>>> type(b)<type 'str'> ~~~ 程序員們經常用一種簡單的說法,把a稱之為數字型變量,意思就是它能夠或者已經跟數字連著呢;把b叫做字符(串)型變量,意思就是它能夠或者已經跟字符串連著呢。 ## 對字符串的簡單操作 對數字,有一些簡單操作,比如四則運算就是,如果3+5,就計算出為8。那么對字符串都能進行什么樣的操作呢?試試吧: ~~~ >>> "py"+"thon"'python' ~~~ 跟我那個不為大多數人認可的發現是一樣的,你還不認可嗎?兩個字符串相加,就相當于把兩個字符串連接起來。(別的運算就別嘗試了,沒什么意義,肯定報錯,不信就試試) ~~~ >>> "py"-"thon"Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str' ~~~ 以上就是對字符串的第一種操作。 ## 連接字符串 在IDLE中按照下面方法操作 ~~~ >>> a = "老齊">>> b= "教python">>> c = a+b>>> print c老齊教python>>> c'\xe8\x80\x81\xe9\xbd\x90\xe6\x95\x99python' ~~~ 這是一種最簡單連接兩個字符串的方法。注意上面例子的最后一行,怎么出現亂碼了?那不是亂碼,是字符編碼的問題。這個你權當沒看見好了。不過的確是看見了。請看官google字符編碼就知道了。這里推薦一篇非常好的文章:[字符集和字符編碼](http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html) > 提示:看官做為學習者,一定要對所學的對象有一種好奇心,比如上面例子中,如果你滿足于print c,發現結果跟自己所預料一樣,這還遠遠不夠。如果你向下走了一行,就發現一個怪怪的結果了,這就讓你在編程路上又前進一大步。所以,要有對世界好奇的心,不斷探索、思考和嘗試。反正在計算機上嘗試,也沒有多大成本。最壞的結果是關掉IDLE罷了。 用`+`號實現連接,的確比較簡單,不過,有時候你會遇到這樣的問題: ~~~ >>> a = 1989>>> b = "free">>> print b+aTraceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: cannot concatenate 'str' and 'int' objects ~~~ 抱錯了,其錯誤原因已經打印出來了(一定要注意看打印出來的信息):`cannot concatenate 'str' and 'int' objects`。原來`a`對應的對象是一個`int`類型的,不能將它和`str`對象連接起來。怎么辦? 可以用下面三種方法中的任何一種: ~~~ >>> print b + `a` #注意,` `是反引號,不是單引號,就是鍵盤中通常在數字1左邊的那個,在英文半角狀態下輸入的符號free1989>>> print b + str(a) #str(a)實現將整數對象轉換為字符串對象free1989>>> print b + repr(a) #repr(a)與上面的類似free1989 ~~~ 可能看官看到這個,就要問它們三者之間的區別了。首先明確,repr()和``是一致的,就不用區別了。接下來需要區別的就是repr()和str,一個最簡單的區別,repr是函數,str是跟int一樣,一種對象類型。不過這么說是不能完全解惑的。幸虧有那好的google讓我輩使用,你會找到不少人對這兩者進行區分的內容,我推薦這個: > 1. When should i use str() and when should i use repr() ? > Almost always use str when creating output for end users. > 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. > 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. > 2.In which cases i can use either of them ? > Well, you can use them almost anywhere. You shouldn't generally use them except as described above. > 3.What can str() do which repr() can't ? > 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. > 4.What can repr() do which str() can't > 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. > 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. 以上英文內容來源:[http://stackoverflow.com/questions/19331404/str-vs-repr-functions-in-python-2-7-5](http://stackoverflow.com/questions/19331404/str-vs-repr-functions-in-python-2-7-5) ## Python轉義字符 在字符串中,有時需要輸入一些特殊的符號,但是,某些符號不能直接輸出,就需要用轉義符。所謂轉義,就是不采用符號現在之前的含義,而采用另外一含義了。下面表格中列出常用的轉義符: | 轉義字符 | 描述 | |-----|-----| | \ | (在行尾時) 續行符 | | \ | 反斜杠符號 | | \' | 單引號 | | \" | 雙引號 | | \a | 響鈴 | | \b | 退格(Backspace) | | \e | 轉義 | | \000 | 空 | | \n | 換行 | | \v | 縱向制表符 | | \t | 橫向制表符 | | \r | 回車 | | \f | 換頁 | | \oyy | 八進制數,yy代表的字符,例如:\o12代表換行 | | \xyy | 十六進制數,yy代表的字符,例如:\x0a代表換行 | | \other | 其它的字符以普通格式輸出 | 以上所有轉義符,都可以通過交互模式下print來測試一下,感受實際上是什么樣子的。例如: ~~~ >>> print "hello.I am qiwsir.\ #這里換行,下一行接續... My website is 'http://qiwsir.github.io'."hello.I am qiwsir.My website is 'http://qiwsir.github.io'.>>> print "you can connect me by qq\\weibo\\gmail" #\\是為了要后面那個\you can connect me by qq\weibo\gmail ~~~ 看官自己試試吧。如果有問題,可以聯系我解答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看