日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python获得用户输入的一个字符串(长度3)_python3 字符串属性(一)

發(fā)布時(shí)間:2025/3/15 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python获得用户输入的一个字符串(长度3)_python3 字符串属性(一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

5、字符串編解碼

{

字符串在Python內(nèi)部的表示是unicode編碼,因此,在做編碼轉(zhuǎn)換時(shí),通常需要以unicode

作為中間編碼,即先將其他編碼的字符串解碼(decode)成unicode,再從unicode編(encode)

成另一種編碼。但是,Python 2.x的默認(rèn)編碼格式是ASCII,就是說,在沒有指定Python源碼編碼

格式的情況下,源碼中的所有字符都會被默認(rèn)為ASCII碼。也因?yàn)檫@個(gè)根本原因,在Python 2.x中

經(jīng)常會遇到UnicodeDecodeError或者UnicodeEncodeError的異常。

原則

decode early, unicode everywhere, encode late,即:在輸入或者聲明字符串的時(shí)候,

盡早地使用decode方法將字符串轉(zhuǎn)化成unicode編碼格式;然后在程序內(nèi)使用字符串的時(shí)候統(tǒng)一使用

unicode格式進(jìn)行處理,比如字符串拼接、字符串替換、獲取字符串的長度等操作;最后,在輸出字符

串的時(shí)候(控制臺/網(wǎng)頁/文件),通過encode方法將字符串轉(zhuǎn)化為你所想要的編碼格式,比如utf-8等。

https://segmentfault.com/a/1190000002966978

}

S.encode(encoding='utf-8', errors='strict') -> bytes 以encoding指定的編碼格式對字符串進(jìn)行編碼,輸出的字節(jié)不是字符串,類型不同,屬性不同。

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 ??檢測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 ? ??沒有找到返回ValuueError錯(cuò)誤??????? (返回左側(cè)第一個(gè))

Like S.find() but raise ValueError when the substring is not found. ? 沒有找到返回 -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 字符串属性(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。