Python strings, lists, and tuples

code show as below:

#指定下标
string_1 = 'list'
print(string_1[0])
#运行结果:l

print(string_1[-1])
#运行结果:t

#字符串的拼接
string_1 = '你好,'
string_2 = '世界!'

string_3 = string_1 + string_2
print(string_3)
#运行结果:你好,世界!



#元组的拼接
tuple_1 = ('abc','def')
tuple_2 = ('123','456')

tuple_3 = tuple_1 + tuple_2
print(tuple_3)
#运行结果:('abc', 'def', '123', '456')

#列表的拼接
list_1 = ['abc','def']
list_2 = ['123','789']
list_3 = list_1 + list_2
print(list_3)
#运行结果:['abc', 'def', '123', '789']

#可以通过列表的下标修改列表的值
list_3[2] = '555'
print(list_3)
#运行结果:['abc', 'def', '555', '789']

#列表还可以单独再末尾添加元素
list_3.append('_i_')
print(list_3)
#运行结果:['abc', 'def', '555', '789', '_i_']

 

Published 163 original articles · Liked 183 · Visit 120,000+

Guess you like

Origin blog.csdn.net/qq_31339221/article/details/98675146