链表list和字符串string

 

1 x = [1, 'a', 2, 'b']
2 type(x)

list

 

%添加元素
1
x.append(3.3) 2 print(x)
[1, 'a', 2, 'b', 3.3]

%遍历链表
1
for item in x: 2 print(item)
1
a
2
b
3.3

%使用索引进行链表遍历
1
i=0 2 while( i != len(x) ): 3 print(x[i]) 4 i = i + 1
1
a
2
b
3.3

%使用+链接链表
1
[1,2] + [3,4]
[1, 2, 3, 4]

%使用*重复链表
1
[1]*3
[1, 1, 1]

%使用in判断链表中是否存在元素
1
1 in [1, 2, 3]
True

字符串
%使用[]来对字符串进行分段
1
x = 'This is a string' 2 print(x[0]) #first character 3 print(x[0:1]) #first character, but we have explicitly set the end character 4 print(x[0:2]) #first two characters
T
T
Th

%-1返回最后字符串最后一个元素
1
x[-1]
'g'

1 x[-4:-2]
'ri'

1 x[:3]
'Thi'

1 x[3:]
's is a string'

1 firstname = 'Christopher'
2 lastname = 'Brooks'
3 
4 print(firstname + ' ' + lastname)
5 print(firstname*3)
6 print('Chris' in firstname)
Christopher Brooks
ChristopherChristopherChristopher
True

%通过特殊字符拆分字符串
1
firstname = 'Christopher Arthur Hansen Brooks'.split(' ')[0] # [0] selects the first element of the list 2 lastname = 'Christopher Arthur Hansen Brooks'.split(' ')[-1] # [-1] selects the last element of the list 3 print(firstname) 4 print(lastname)
Christopher
Brooks

%字符串合并
1
'Chris' + '2'
'Chris2'

%字符串转换
1
'Chris' + str(2)
'Chris2'









 

转载于:https://www.cnblogs.com/zhengzhe/p/8510810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值