Python教程: Python 介紹

來源:互聯網
上載者:User

標籤:message   control   運算式   字串   分行符號   

Python教程: Python 介紹


1、Python 命令列解釋提示符下

輸入control + p 命令提示字元向上尋找

輸入control + n 命令提示字元向下尋找


2、在互動模式中,最後列印的運算式的值被賦予給變數_


3、在字串第一個引號前添加r字元,可以避免通過\逸出字元

print r‘C:\some\name‘


4、使用三個引號包含的字串可以跨越多行

“””…””"

‘’’…’‘‘


註:字串的首行將自動包含行的結尾分行符號,通過在行首添加\可以避免

print """\

Usage: thingy [OPTIONS]

     -h                        Display this usage message

     -H hostname               Hostname to connect to

"""


5、字串串連 (+, *)

‘abc‘ + ‘def‘# 字串串連,可以串連變數

‘abc‘ * 3 # 字串重複

‘Py‘ ‘thon‘# 兩個字串字面值自動連接,不包括變數或運算式


# 字串串連

>>> text = (‘Put several strings within parentheses ‘

            ‘to have them joined together.‘)

>>> text

‘Put several strings within parentheses to have them joined together.‘


6、字串索引

字串的下標從0開始索引,字串是沒有分割字元的類型,一個字元是一個簡單的長度為1字串

>>> word = ‘Python‘

>>> word[0]  # character in position 0

‘P‘


7、負數從字串右側開始計數

>>> word[-1]  # last character

‘n‘

註:-0相當於0,負數從-1開始


8、字串支援切片,索引擷取單個字元,切片擷取子字串

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)

‘Py‘

>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)

‘tho‘

註:切片的開始參數總是被包含,結尾總是被排除的。


9、字串切片預設值,第一個索引省去預設為0,第二個索引省去預設為切片的長度;

>>> word[:2]  # character from the beginning to position 2 (excluded)

‘Py‘

>>> word[4:]  # characters from position 4 (included) to the end

‘on‘

>>> word[-2:] # characters from the second-last (included) to the end

‘on‘


10、最簡單理解字串切片原理是記住字元之間的位置,左側的字元是0,右側的字元是n就是索引n:

 +---+---+---+---+---+---+

 | P | y | t | h | o | n |

 +---+---+---+---+---+---+

 0   1   2   3   4   5   6

-6  -5  -4  -3  -2  -1


11、使用較大的索引將會出現如下錯誤

>>> word[42]  # the word only has 7 characters

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

IndexError: string index out of range


12、Python字串是不可以被修改的,給字串索引位置賦值將會出現如下錯誤!

>>> word[0] = ‘J‘

  ...

TypeError: ‘str‘ object does not support item assignment

>>> word[2:] = ‘py‘

  ...

TypeError: ‘str‘ object does not support item assignment


# 如果需要,你可以建立一個新的字串。


13、Python 2.0以後引入了新的儲存文本的資料類型,Unicode對象。他可以很好的儲存、維護Unicode資料並提供自動轉換。

Unicode常被用來解決國際化。


14、Unicode字串的建立

>>> u‘Hello World !‘

u‘Hello World !‘


# 字串前面的小寫u是被支援用來建立Unicode字元的,如果你想使用特殊字元,請參考Unicode-Escape。例如:

>>> u‘Hello\u0020World !‘

u‘Hello World !‘


註:\u0020表示Unicode字元0x0020(空格)


15、










本文出自 “辨楓拂雪” 部落格,請務必保留此出處http://cnhttpd.blog.51cto.com/155973/1660235

Python教程: Python 介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.