<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    習題 6: 字符串(string)和文本?

                    雖然你已經在程序中寫過字符串了,你還沒學過它們的用處。在這章習題中我們將使用復雜的字符串來建立一系列的變量,從中你將學到它們的用途。首先我們解釋一下字符串是什么 東西。

                    字符串通常是指你想要展示給別人的、或者是你想要從程序里“導出”的一小段字符。Python 可以通過文本里的雙引號 " 或者單引號 ' 識別出字符串來。這在你以前的 print 練習中你已經見過很多次了。如果你把單引號或者雙引號括起來的文本放到 print 后面,它們就會被 python 打印出來。

                    字符串可以包含格式化字符 %s,這個你之前也見過的。你只要將格式化的變量放到字符串中,再緊跟著一個百分號 % (percent),再緊跟著變量名即可。唯一要注意的地方,是如果你想要在字符串中通過格式化字符放入多個變量的時候,你需要將變量放到 ( ) 圓括號(parenthesis)中,而且變量之間用 , 逗號(comma)隔開。就像你逛商店說“我要買牛奶、面包、雞蛋、八寶粥”一樣,只不過程序員說的是”(milk, eggs, bread, soup)”。

                    我們將鍵入大量的字符串、變量、和格式化字符,并且將它們打印出來。我們還將練習使用簡寫的變量名。程序員喜歡使用惱人的難度的簡寫來節約打字時間,所以我們現在就提早學會這個,這樣你就能讀懂并且寫出這些東西了。

                     1
                     2
                     3
                     4
                     5
                     6
                     7
                     8
                     9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    x = "There are %d types of people." % 10
                    binary = "binary"
                    do_not = "don't"
                    y = "Those who know %s and those who %s." % (binary, do_not)
                    
                    print x
                    print y
                    
                    print "I said: %r." % x
                    print "I also said: '%s'." % y
                    
                    hilarious = False
                    joke_evaluation = "Isn't that joke so funny?! %r"
                    
                    print joke_evaluation % hilarious
                    
                    w = "This is the left side of..."
                    e = "a string with a right side."
                    
                    print w + e
                    

                    你應該看到的結果?

                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    $ python ex6.py
                    There are 10 types of people.
                    Those who know binary and those who don't.
                    I said: 'There are 10 types of people.'.
                    I also said: 'Those who know binary and those who don't.'.
                    Isn't that joke so funny?! False
                    This is the left side of...a string with a right side.
                    $
                    

                    加分習題?

                    1. 通讀程序,在每一行的上面寫一行注解,給自己解釋一下這一行的作用。
                    2. 找到所有的”字符串包含字符串”的位置,總共有四個位置。
                    3. 你確定只有四個位置嗎?你怎么知道的?沒準我在騙你呢。
                    4. 解釋一下為什么 we+ 連起來就可以生成一個更長的字符串。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 5: 更多的變量和打印

                    Next topic

                    習題 7: 更多打印

                    This Page

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频