- “+”连接
str_name1 = 'To' str_name2 = 'ny' str_name = str_name1 + str_name2 print(str_name)
运行结果:Tony
- join连接
这个函数接受一个列表或元组,然后用字符串依次连接列表中每一个元素。
list1 = ['P', 'y', 't', 'h', 'o', 'n']
print("".join(list1))
运行结果:Python
str_name1 = 'To' str_name2 = 'ny' str_name = str_name1.join(str_name2) print(str_name)
需要注意,运行结果为:nToy
- “,”连接
a, b = 'Hello', 'word' c = a, b print(a, b) print(c)
运行结果:
Hello word
(‘Hello’, ‘word’)
- format连接
str_word_keyword = 'hellow, world!{a},{b}'.format(b='张三', a='李四')
print(str_word_keyword)
运行结果:
hellow, world!李四,张三
- np.append
import numpy as np a = np.array([[1, 2],[3, 4],[5, 6]]) b = np.array([[11, 22],[33, 44],[55, 66]]) c = np.append(a,b) print(c)
运行结果:[ 1 2 3 4 5 6 11 22 33 44 55 66]
- np.concatenate
import numpy as np a = np.array([[1, 2],[3, 4],[5, 6]]) b = np.array([[11, 22],[33, 44],[55, 66]]) c = np.concatenate((a,b),axis=0) print(c) d = np.concatenate((a,b),axis=1) # 同 e = np.concatenate([a,b],axis=1) print(d)
运行结果:
[[ 1 2]
[ 3 4]
[ 5 6]
[11 22]
[33 44]
[55 66]]
[[ 1 2 11 22]
[ 3 4 33 44]
[ 5 6 55 66]]
- np.hstack 和 np.vstack
import numpy as np a = np.array([[1, 2],[3, 4],[5, 6]]) b = np.array([[11, 22],[33, 44],[55, 66]]) # 行连接:等价于 np.concatenate((a,b),axis = 1) c = np.hstack((a,b)) print(c) # 列连接:等价于 np.concatenate((a,b),axis = 0) d = np.vstack((a,b)) print(d)
运行结果:
[[ 1 2 11 22]
[ 3 4 33 44]
[ 5 6 55 66]]
[[ 1 2]
[ 3 4]
[ 5 6]
[11 22]
[33 44]
[55 66]]



