二、數字型別及其操作
三、字串及其操作
值改變,但是id不變,證明其實就是在改變原值,即 原值可變,是可變型別
值改變,id也跟著變,證明其實是產生了新值,原值不可變
1.1 作用:
記錄整數狀態的事物,如:年齡、號碼人數等
1.2 定義:
age =
18# age = int(18)
int 可以將純數字的字串轉化成整型(兩邊可以有空格,中間不行)
1.3 常用操作和內建方法
算術運算 + 比較運算
2.1 作用
記錄薪資、身高、體重等
2.2 定義
length =
4.76
# length = float(4.76)
float 可以將浮點型數字組成的字串轉化成浮點型
2.3 常用操作和內建方法
算術運算 + 比較運算
總結:數字型別只能存乙個值,屬於不可變型別
記錄描述性質的狀態,如:姓名、住址等
在引號(' '、" "、''' '''、""" """)內包含一串或者多行字串
資料型別轉換:str()可以把任意型別轉換成字串型別
(1)按索引取值(正向取、反向取,不可改)
s =
"hello"
print
(s[0])
print
(s[-1]
)
(2)切片操作
顧頭不顧尾,有步長,屬於拷貝操作,不改變原值的id
msg =
'hello world'
print
(msg[0:
4])# hell 預設步長為1,取第1到第4個字元, 原字串不變
print
(msg[:6
:2])
# hlo 省略起始位置,預設起始位置為0
print
(msg[::
2])# hlowrd 省略結束位置,預設一直到末尾全部都取到
print
(msg[::
])# hello world 可簡寫成print(msg[:]) 省略步長,預設步長為1
(3)長度len()
msg =
'hello'
print
(len
(msg)
)
(4)成員運算in 、 not in
msg =
"hello 你好"
print
("你好"
in msg)
print
("你好"
notin msg)
# 語義明確,推薦使用
print
(not
"你好"
in msg)
# 不推薦使用
(5)a. 移除空白strip
msg =
' hello \n '
res1 = msg.strip(
)# 預設移除兩邊的空白字元,包括\n 和 \t,不改變原來的msg
b. 移除兩邊非空白字元
msg =
"==hello!()"
res = msg.strip(
"()=!"
)print
(res)
# 得到的是新字串,不改變原來的msg
(6)切分split:將字串按照某個分隔符切分成乙個列表
msg =
"a:b:c:d"
# 字串按冒號有規律的分布
res = msg.split(
':')
print
(res)
# ['a', 'b', 'c', 'd']
(7)迴圈(可以被for迴圈遍歷)
for item in
"hello"
:print
(item)
(1)strip,lstrip,rstrip
msg =
"*****hello*****"
print
(msg.strip(
'*')
)# hello 去除兩邊的*號
print
(msg.lstrip(
'*')
)# hello***** 去除左邊的*號
print
(msg.rstrip(
'*')
)# *****hello 去除右邊的*號
(2)lower、upper、(swapcase)
msg =
"aabb"
print
(msg.lower())
# aabb 所有字母全部小寫
print
(msg.upper())
# aabb 所有字母全部大寫
print
(msg.swapcase())
# aabb 所有的字母大小寫反轉
(3)startswith、endswith
返回布林值
msg =
"tomorrow is tuesday"
print
(msg.startswith(
"to"))
# true 判斷是否是以to開頭
print
(msg.endswith(
"y")
)# true 判斷是否是以y結尾
(4)format三種用法
以下**均可實現格式化輸出"my name is jason,age is 18"
# ① %s的方式
print
("my name is %s, age is %s"%(
"jason",18
))# ② format的方式
print
("my name is {}, age is {}"
.format
("jason",18
))print
("my name is , age is "
.format
(y=18
, x=
"jason"))
print
("my name is , age is "
.format
("jason",18
))# 按照位置
# ③ f''的方式
x ='jason'
y =18
print
(f"my name is , age is "
)#python3.6版本及之後支援
了解:
① f 搭配{}可以執行字串中的**
f
② f 包含的字串可以放到多行
name =
'jason'
age =
18res = f'my name is ' \
f'age is '
③ {}內不能有#和/
print
('age is }'
)# ==>age is
類似的:
n =
5print
('房貸利率減少了%s%%'
% n)
# ==>房貸利率減少了5%
(5)split、rsplit
msg =
'a: b: c: d'
print
(msg.split(
':',1)
)# ['a', 'b: c: d] 正向分割一次
print
(msg.rsplit(
':',1)
)# ['a: b: c', 'd'] 反向分割一次
(6)join:將列表中的元素以指定字元連線組合成乙個字串
l =
['a'
,'b'
,'c'
,'d'
]print
("-"
.join(l)
)# a-b-c-d
(7)replace:替換指定字串
msg =
'cc cc cc'
print
(msg.replace(
'cc'
,'cc',2
))# cc cc cc (將前兩個"cc"替換成"cc")
print
(msg.replace(
'cc'
,'c'))
# c c c (將所有的cc都替換成c)
(8)isdigit:判斷字串是否是由純數字組成的
age =
" 18**"
print
(age.isdigit())
# false (必須純數字才是true,小數點、空格都不能有)
str型別總結:
(1)只能存乙個值
(2)有序
(3)屬於不可變型別
字串型別及其操作
字串是字元的有序序列。由一對單引號或雙引號表示,僅表示單行字串 由一對三引號表示,可表示多行字串,也是一種注釋方式 請輸入帶有符號的溫度值 s i 返回字串中第i個字元 s m n 返回字串中 m,n 的字串 m缺失表示至開頭 n確實表示至結尾 s m n k 根據步長k對字串接切片 s 1 5 2...
1162 數字 字串
時間限制 1000 ms 記憶體限制 65535 kb 難度 0 描述 有一行數字 現在定義 0表示空格,即這行數字被分割成若干個數 要求將這些數按照從小到大順序排列,若該行數字全為零 則表示為零,兩個數字之間可能有多個0,開頭和結尾可能都有0,所有的0都看成空格,數字的個數不超過100。輸入輸入有...
數字字串問題
題目描述 輸入兩個很大的正數 用c字串表示 輸出他們的乘積。採用的方法可以模擬手工乘法 關鍵步驟 void multiply const char a,const char b for int i 0 i ca i for int j 0 j cb j s i j 1 a i 0 b j 0 for...