【原創】Python第二章——字串

來源:互聯網
上載者:User

標籤:

    字串是一個字元序列,(提醒:序列是Python的一個重要的關鍵詞),其中存放UNICODE字元。Python中的字串是不可變的(immutable),即對字串執行操作時,總是產生一個新的字串而不是修改現有的字串。

  字串常量的表示 1. 3種表示
1 #單引號2 A = ‘Python‘3 #引號4 B = "Python"5 #三引號6 C = """Python""" 
2. 為什麼要這麼麻煩? (1)當字串中包含了某種引號字元,那麼使用與該引號字元不同的引號包含字串時,可以直接使用該引號,而不用使用逸出字元
1 #單引號不用轉義2 A = "I‘m a Python newcomer"3 #引號不用轉義4 B = ‘I have learnt "C" language‘

 

(2)三引號可以讓輸出的字串直接跨行 當沒有三引號時,不得不這樣做:
A = "I‘m \na newcomer"

 

如果使用三引號,可以不用使用\n\
B = """I‘m anewcomer"""

 

3. 過長字串的跨行表示 建議使用圓括弧將多行字串包括在一起,而不是用轉義符。
1 #建議2 A = ("I‘m "3     "a newcomer")4 #不建議5 B = "I‘m " + 6     "a newcomer"7 C = "I‘m 8      a newcomer"
4. 逸出序列 
逸出字元 含義
\newline 忽略換行
\\ \
\‘
\" "
\a ASCII蜂鳴
\b ASCII退格
\f ASCII走紙
\r 斷行符號CR
\n 換行LF
\t ASCII定位字元
\v ASCII垂直指標
\ooo 給定八進位字元
\xhh 給定8位十六進位字元
\uhhhhh 給定16位十六進位字元
\Uhhhh hhhh 給定32位十六進位字元
\N{name} 給定名稱的Unicode字元,name是一個標準Unicode名稱
字串的比較 1. < <= > >= == !=與C語言的庫函數相似 2. Unicode字元比較存在的問題(1)有些Unocde字元可以使用多種位元組序列表示,比如某些字元可以使用3中不同的UTF-8編碼位元組表示。解決方案:使用unicodedata模組的normalize函數,並將‘NKFD‘作為該函數的第一個實參。
1 import unicodedata2 title = u"Klüft skräms inför på fédéral électoral große"3 B = unicodedata.normalize(‘NFKD‘, title).encode(‘ascii‘,‘ignore‘) 4 print(B)

 

(2)字元排序問題在一些國家中,字母字元的順序不同於英美國家,例如在瑞典語中,a排在z之後。對此Python不進行推測,而是使用字串的記憶體位元組進行比較,此時的排序是基於Unocode字元的,當然我們也可以自訂Python的排序方法。  字串的提取——分區    這和序列的提取——分區是一樣的,這放在下一篇文章講。  字串操作符和方法 1. 操作符(1)比較操作符< <= > >= == !=與C語言的庫函數相似(2)*(複製),*=,+(追加),+=(3)成員關係測試 in 2. 方法    字串實現了所以通用序列的方法,也帶有自己特別的方法,詳見協助文檔,這裡不說。
(1)格式化字串的兩種方法 printf風格字串格式化    %操作符,用于格式化的一個操作符。形式為:
  1. format % values
其中format是一個格式化字串,%用於將後面的values替換前面的格式字串,作用和C語言的sprintf一樣。在format字串中帶有一個以%開頭的轉換標識符。用於指示後面value如何替換前面的format字串。 
1 print(‘number is %d‘%10) 2 將輸出number is 10

 

其中的%d就是一個轉換標識符,表示這裡是一個整數,%d被替換成真實值10。 轉換標識符不僅僅一種,它包含兩個或以上的字元,並且順序化地包含以下內容:(提醒:順序是固定的,但有的是可選)1) 必須以%開頭2)Mapping key(可選),由一對括弧包含的字元序列3)轉換標誌(可選),作用於一些轉換類型的結果4)最小寬度欄位(可選),用於使轉換標識符最後轉換得到的長度達到指定的最小寬度欄位5)精度(可選),含義取決于格式字元6)長度修飾符(可選),修飾整數7)轉換格式字元(必須) 

 

轉換標誌有:
Flag Meaning
‘#‘ 轉換標誌的起始符
‘0‘ 用0在數的欄位寬度內進行填充
‘-‘ 靠左對齊 (如果給出了上面的0轉換和靠左對齊轉換,則忽略0轉換
‘ ‘ (一個空格)在一個有符號轉換中,如果是一個正數,那麼在開頭加上空格而不是‘+‘
‘+‘ 若數為正數,則在開頭添加‘+‘(忽略空格標誌).
最小寬度欄位:    如果字元的數量小於指定的最小寬度欄位,則會在剩餘的地方進行填充(預設是空格,可以由轉換標誌修改填充的字元);如果大於,則原樣輸出。 精度:含義取決于格式字元
 
格式字元 含義
dioux 精度表示最少顯示數字位元,如果精度過大則以0填充,否則原樣輸出
ef 精度表示小數點後的數字位元
g 精度表示最大有效位
s 精度表示最大的字元數
   
長度修飾符:h,l,Lh:說明整數是short型l:說明整數是long型L:當和efg使用時,L說明是long double型 

The conversion types are:

Conversion Meaning Notes
‘d‘ Signed integer decimal.  
‘i‘ Signed integer decimal.  
‘o‘ Signed octal value. (1)
‘u‘ 已廢棄,等同於d (7)
‘x‘ Signed hexadecimal (lowercase). (2)
‘X‘ Signed hexadecimal (uppercase). (2)
‘e‘ Floating point exponential format (lowercase). (3)
‘E‘ Floating point exponential format (uppercase). (3)
‘f‘ Floating point decimal format. (3)
‘F‘ Floating point decimal format. (3)
‘g‘ Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
‘G‘ Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
‘c‘ Single character (accepts integer or single character string).  
‘r‘ String (converts any Python object using repr()). (5)
‘s‘ String (converts any Python object using str()). (5)
‘a‘ String (converts any Python object using ascii()). (5)
‘%‘ 輸出%  

Notes:

  1. The alternate form causes a leading zero (‘0‘) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.當使用了轉換標誌0,那麼填充0的時候,填充的位置將是0o和數字之間,比如%#08o‘ % 34的結果是0o000042

  2. The alternate form causes a leading ‘0x‘ or ‘0X‘ (depending on whether the ‘x‘ or ‘X‘ format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.當使用了轉換標誌0,那麼填充0的時候,填充的位置將是0x和數字之間,比如%#08x‘ % 34的結果是0x000022。

  3. The alternate form causes the result to always contain a decimal point, even if no digits follow it.

    The precision determines the number of digits after the decimal point and defaults to 6.精度控制決定了小數點後的位元,預設是6位

  4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.

    The precision determines the number of significant digits before and after the decimal point and defaults to 6.

  5. If precision is N, the output is truncated to N characters.如果上面指定的精度是N,那麼字串的最大長度是N

  1. See PEP 237.

Since Python strings have an explicit length, %s conversions do not assume that ‘\0‘ is the end of the string.

因為Python的String有顯式的長度,所以%s轉換不認為‘\0‘是字串的結尾

Changed in version 3.1: %f conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g conversions.


本文連結:http://www.cnblogs.com/cposture/p/4722340.html

【原創】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.