python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)
5、字符串編解碼
{
字符串在Python內(nèi)部的表示是unicode編碼,因此,在做編碼轉(zhuǎn)換時(shí),通常需要以u(píng)nicode
作為中間編碼,即先將其他編碼的字符串解碼(decode)成unicode,再?gòu)膗nicode編(encode)
成另一種編碼。但是,Python 2.x的默認(rèn)編碼格式是ASCII,就是說(shuō),在沒(méi)有指定Python源碼編碼
格式的情況下,源碼中的所有字符都會(huì)被默認(rèn)為ASCII碼。也因?yàn)檫@個(gè)根本原因,在Python 2.x中
經(jīng)常會(huì)遇到UnicodeDecodeError或者UnicodeEncodeError的異常。
原則
decode early, unicode everywhere, encode late,即:在輸入或者聲明字符串的時(shí)候,
盡早地使用decode方法將字符串轉(zhuǎn)化成unicode編碼格式;然后在程序內(nèi)使用字符串的時(shí)候統(tǒng)一使用
unicode格式進(jìn)行處理,比如字符串拼接、字符串替換、獲取字符串的長(zhǎng)度等操作;最后,在輸出字符
串的時(shí)候(控制臺(tái)/網(wǎng)頁(yè)/文件),通過(guò)encode方法將字符串轉(zhuǎn)化為你所想要的編碼格式,比如utf-8等。
https://segmentfault.com/a/1190000002966978
}
S.encode(encoding='utf-8', errors='strict') -> bytes 以encoding指定的編碼格式對(duì)字符串進(jìn)行編碼,輸出的字節(jié)不是字符串,類(lèi)型不同,屬性不同。
6.? S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
1 >>> a='hello world!'
2 >>> a.endswith('!')3 True4 >>> a.endswith('o',0,5)5 True6 >>> a.startswith('h')7 True8 >>> a.startswith('w',6)9 True10 >>> a.startswith('hello')11 True
7、?S.expandtabs(tabsize=8) -> str ?把字符串的tab字符(\t)轉(zhuǎn)化為空格,如不指定tabsize,默認(rèn)為8個(gè)空格
1 >>> a='hello world'
2 >>>a.expandtabs()3 'hello world'
4 >>> a='\t hello world \t'
5 >>>a.expandtabs()6 'hello world'
7 >>> a.expandtabs(tabsize=2)8 'hello world'
8、S.find(sub[, start[, end]]) -> int ??檢測(cè)sub是否在字符串中,如果在則返回index,否則返回-1,start,end為可選參數(shù),決定范圍。(返回左側(cè)第一個(gè))
S.rfind(sub[, start[, end]]) -> int? (返回右側(cè)第一個(gè))
Return the highest index in S where substring sub is found,
such that sub is contained within S[start:end].? Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
1 >>> a='hello world'
2 >>> a.find('h')3 0
4 >>> a.find('h',1,3)5 -1
6 >>> a.find('o',1,4)7 -1
8 >>> a.find('o',1,5)9 4
9、
S.index(sub[, start[, end]]) -> int ? ??沒(méi)有找到返回ValuueError錯(cuò)誤??????? (返回左側(cè)第一個(gè))
Like S.find() but raise ValueError when the substring is not found. ? 沒(méi)有找到返回 -1
S.rindex(sub[, start[, end]]) -> int????????? (返回右側(cè)第一個(gè))
Like S.rfind() but raise ValueError when the substring is not found.
1 >>>a2 'hello world'
3 >>> a.index('ll')4 2
5 >>> a.index('lll')6 Traceback (most recent call last):7 File "", line 1, in
8 ValueError: substring not found
10、S.isalnum() -> bool??Return True if all characters in S are alphanumeric
都是字母和數(shù)字字符返回True,否則返回False
>>> a.isalnum()
False
>>> a='123#$":,./'
>>> a.isalnum()
False
>>> a='123'
>>> a.isalnum()
True
>>> a='123a'
>>> a.isalnum()
True
>>> a='123a('
>>> a.isalnum()
False
11、S.isalpha() -> bool Return True if all characters in S are alphabetic
判斷字符串是否為字母
>>> a='123'
>>> b='123a'
>>> c='abc'
>>> a.isalpha()
False
>>> b.isalpha()
False
>>> c.isalpha()
True
總結(jié)
以上是生活随笔為你收集整理的python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 太想成功了--成功之前,你是否养成了这些
- 下一篇: 对extern C的一点小认识