1. 程式人生 > >09 python初學 (字串)

09 python初學 (字串)

# 重複輸出字串
print('hello' * 2)
# >>>hellohello

# 字串切片操作
print('hello'[2:])
# >>>llo

# 關鍵字 in
print('ll' in 'hello')
# >>> True

# 字串拼接

# 不推薦使用此種方式,方式一
a = '123'
b = 'abc'
c = 'haha'
d = a + b
print(d)
# >>> 123abc
# 方式二join,字串的拼接: '拼接字串'.join([a, b]),將後面列表裡的a,b用前面的拼接字串拼接起來
d = ''.join([a, b]) print(d) # >>>123abc d = '---'.join([a, b, c]) print(d) # >>>123---abc---haha