iT邦幫忙

DAY 4
0

Python初學起步走系列 第 4

[Python初學起步走-Day4] - 字串與切片

Python中字串是通過str資料型態表示,是Unicode字符序列

用引號建立字串,可使用以下三種方式

1.單引號:

text ='to be or not to be'

2.雙引號:

text ="to be or not to be"

3.三個引號:

text ='''to be or not to be''' text ="""to be or not to be"""

可以自用選用,但必須兩端一制

text ="allows embedded 'single' quotes"

切片

當要從一個序列裡提取個別資料項,可以使用存取運算符([])

text = "taipei city"

下圖表示其索引位置

切片運算符使用

text[start_index: end_index: step]

start_index:表示開始位置,預設為0(step為正)或 -len(text)(step為負)
end_index:表示結束位置,預設為len(text)-1(step為正)或-1
step:步進值預設為1,不可為0

text = "taipei city"
print(text[-1:2:-2])
print(text[::2])
print(text[::-1])


上一篇
[Python初學起步走-Day3] - 數值資料型態與運算符
下一篇
[Python初學起步走-Day5] - 字串方法
系列文
Python初學起步走30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言