python語言中如何判斷乙個字串有多少位?

時間 2021-06-06 18:05:42

1樓:

Python2的str:編碼後的字串,在C中就是char*,直接用len()查的長度便是位元組數

Python3的str:可能是UTF-16或UTF-32編碼的字串,在C中是wchar_t*,通常不要關心它的編碼,而抽象理解為編碼前的字串。所佔位元組數是len()的長度乘以編譯Python所用的編譯器的wchar_t的長度。

2樓:

(假設題主問的是位是bit)

python裡的字串,就是下面那樣的obejct。

"i am a string", '

i am also a string',

u"i am a unicode stiring"

python中沒有char型別,只有字串。用包含單個char的string來表示char。根據python的文件,乙個string由char組成,乙個char至少有乙個byte,就是8個bit。

如果是unicode string,那麼每個字元可能有16-32 bit (<=sys.maxunicode).

摘自https://

docs.python.org/2/reference/datamodel.htm

Strings

The items of a string are characters. There is no separate character type; a character is represented by a string of one item. Characters represent (at least) 8-bit bytes.

The built-in functions chr() and ord() convert between characters and nonnegative integers representing the byte values. Bytes with the values 0-127 usually represent the corresponding ASCII values, but the interpretation of values is up to the program. The string data type is also used to represent arrays of bytes, e.

g., to hold data read from a file.

Unicode

The items of a Unicode object are Unicode code units. A Unicode code unit is represented by a Unicode object of one item and can hold either a 16-bit or 32-bit value representing a Unicode ordinal (the maximum value for the ordinal is given in sys.maxunicode, and depends on how Python is configured at compile time).

Surrogate pairs may be present in the Unicode object, and will be reported as two separate items. The built-in functions unichr() and ord() convert between code units and nonnegative integers representing the Unicode ordinals as defined in the Unicode Standard 3.0.

Conversion from and to other encodings are possible through the Unicode method encode() and the built-in function unicode().

python 如何判斷兩個時間是否在同乙個5分鐘時段內?

無關風月 用 unix 時間戳除以300的得數是否一致來判斷,相同的即在同乙個時間段,否則不在。300 是 5 分鐘的秒數,然後中國的時區 8 小時剛好是 5 分鐘的倍數,所以不影響。In 43 time list 09 56 00 09 59 59 10 00 00 10 01 01 10 06 ...

python語言的強制縮排是乙個敗筆?

張迪 自從用了py,不管是c還是vb,就算是有了括號,我還是喜歡加個table。層層遞進真的確實賞心悅目,也好查詢。如果覺得是缺點,請多用幾個函式,邏輯更清晰一點。 是的,但是和強迫優雅之類的無關,而是強制縮排導致了語法上無法實現匿名函式,別跟我講只能寫一行的lambda,也別跟我安利其它什麼東西,...

Python是乙個業餘的程式語言嗎?

Python 只是個工具,幫助我們解決問題的工具每個工具都會有其閃光的一面,自然也有其不足的地方,比如錘子用來敲,卻不適合切削 只有業餘的人,沒有業餘的工具 Python雖然入門相對容易些,學精一樣需要投入時間和精力。網上那些把說python學個幾天就上天的,笑笑就好 孟佳明 我們一般不用業餘來修飾...