python編程基礎之二十

來源:互聯網
上載者:User

標籤:機器   form   系統預設   方法   8進位   precision   寬度   for   enc   

字串的其他常用方法:

ord(char)  # 返回char字元對應的碼值,可以是中文字元

chr(x)  # 輸入一個unicode碼,返回對應的字元

eval(str)  # 將str 中的內容以代碼的形式返回

repr(x) 返回對象的str格式,適合機器執行

字串格式化

%格式化:

%[flags][width][.precision]typecode

format格式化:

[fill][align][sign][#][width][.precision][type]

fill :填充符號

align : 對齊  < 靠左對齊,> 靠右對齊  ,  ^  置中對齊

sign : 顯示符號,+顯示正號,-顯示符號

# : 對於2,4,8進位會顯示0b,0o,0x

width:寬度

,千分位

.precision : 精度

type : s 字串  d 整形  f 浮點型

位元組:

建立位元組:

b1 = b‘hello‘ # 在字串前加一個b

 

b2 = byte("中文",‘utf-8‘)

設定編碼格式:

#-*- coding: utf-8 -*-

import sys

sys.setdefaultencoding(‘utf-8‘) #修改系統預設編碼格式

字串轉位元組:  ‘中文‘.endcode(‘utf-8‘)

位元組轉字串: b‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘)

 

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.