1.索引
# 直接使用編號(hào)訪問各個(gè)元素
greeting = 'Hello'
print(greeting[0])
# 訪問最后一個(gè)元素
print(greeting[-1])
# 可直接對(duì)序列進(jìn)行索引操作
print('Hello'[0])
# 對(duì)函數(shù)返回的序列進(jìn)行索引操作
name = input("請(qǐng)輸入你的名字:")[0]
print(name)
========================1=========================
H
o
H
請(qǐng)輸入你的名字:zenos
z
2.切片
# 簡(jiǎn)單切片 包含開始,不包含結(jié)尾
tag = '<a >Python web site</a>'
print(tag[9:30])
print(tag[32:-4])
# 正向前一位必須比后一位大
numbers = [1,2,3,4,5,6,7,8,9,10]
print(numbers[7:10])
print(numbers[-3:0])
# 切片到末尾
print(numbers[0:])
# 切片從開頭開始
print(numbers[:2])
# 復(fù)制整個(gè)序列
print(numbers[:])
# 正向步長(zhǎng)
print(numbers[1:7:2])
# 反向步長(zhǎng) 后一位必須比前一位大
print(numbers[8:3:-1])
========================2=========================
http://www.python.org
Python web site
[8, 9, 10]
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6]
[9, 8, 7, 6, 5]
3.序列相加
# 同類型序列相加等于拼接
print([1,2,3]+[4,5,6])
print('hello' + 'world')
# 不同類型之間不可以進(jìn)行相加
# print([1,2,3]+'hello')
========================3=========================
[1, 2, 3, 4, 5, 6]
helloworld
4.序列相乘(重復(fù)序列x次來創(chuàng)建新序列)
print('python'*5)
print([None]*5)
========================4=========================
pythonpythonpythonpythonpython
[None, None, None, None, None]
5.簡(jiǎn)單的例子
sentence = input("Setence:")
screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) // 2
print()
print(' ' * left_margin + '+' + '-' * (box_width - 2) + '+')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '| ' + sentence + ' |')
print(' ' * left_margin + '| ' + ' ' * text_width + ' |')
print(' ' * left_margin + '+' + '-' * (box_width - 2) + '+')
========================5=========================
Setence:hello world
+---------------+
| |
| hello world |
| |
+---------------+
6.成員資格 檢查對(duì)象是否是序列的成員
permission = 'wx'
print('w' in permission)
========================6=========================
True