python 字串基本操作001

2021-10-11 01:33:48 字數 1647 閱讀 4349

序列 包括 列表 list (空列表為),元組( 單個元素 為 (1,)),字串(空字元為』』),字串行的一種。

『,』.format(8020.99999) format格式化輸出;佔位符合%s(字元),%d(整數),%f(浮點數) ,』%s , %d』 %(『abc』,17)。

**如下(示例):

#字元簡單操作:

#除去前後字元strip()

'#hello#'

.strip()

a ='1,2,3,4,5'

#分割符 list

b = a.

split

(','

)print

(b)'&'

.join

(b)print

('&'

.join

(b))

#出現次數

print

(a.count

('a'))

#find 找不到(-1) ,index (異常)

a ='hello'

print

(a.find

('ll'))

print

(a.index

('ll'

)) #左到右

print

(a.find

('ll'

)) # 右到左

#print(a.index('a')) # 異常

**如下(示例):

# 格式化輸出% format

# %s %d %f %

08d 8位,左邊用0填充

# %05.2f

,包括小數點5位,左邊用0填充,小數點儲存2位

# 多個要用元組 %(1

,2,3

)print

('%08d 8位,左邊用0填充'%9

) #00000009

print

('%05.2f,包括小數點5位,左邊用0填充,小數點儲存2位'

%1.666666

) #01.67

# python中format函式用於字串的格式化 ^<> 居中,左對齊,右

# 30 字段長度 最左到最右之間的長度

print(''

.format

('liyiman'))

#精度控制 :

.2f 兩位 .5f

5位不足補0

print(''

.format

(245.7890

)) #245.79

print(''

.format

(245.7890

))#245.7890

print(''

.format

(245.7890

))#00245.79

#千位符號 :

,print(''

.format

(100000000

)) #100

,000

,000

#進製轉換 :b/

:o/:d/

:xprint

('二進位制,八進位制,十進位制,十六進製制'

.format(20

))

python 字串基本操作

字串的基本操作 import operator s hello,world 去掉空格 換行符 s.strip 從左側切掉 s.lstrip hello,從右測切掉 a s.rstrip world 字條串拼接 s2 to me a s s2 查詢第乙個出現該字元的位置 a s.index o a s...

python 字串基本操作

一 引號 單引號 雙引號 三引號內都是字串,三引號的支援換行 字串內本身需要輸出引號,需要用反斜槓進行轉義,也可以直接在前面加個 r 例如 print r asd asd asd qwe 輸出 asd asd asd qwe 二.下標 索引 從0開始,用 0 框住 name yexueqing pr...

Python字串的基本操作

str字串有哪些操作 mystr.find str,start,end 如果存在,返回下標,不存在返回 1 mystr.index str,start,end 如果存在,返回下標,不存在報異常 mystr.count str,start,end 返回str在start到end之間出現的次數 myst...