字串是以單引號'或雙引號"括起來的任意文本
創建字串:
st = 'Hello World!'
py = 'Hello Python'
對應操作:
# 1 * 重復輸出字串 print('hello'*2) # 2 [] ,[:] 通過索引獲取字串中字符,這里和串列的切片操作是相同的,具體內容見串列 print('hello world'[2:]) # 3 in 成員運算子 - 如果字串中包含給定的字符回傳 True print('el' in 'hello') # 4 % 格式字串 name = make print('make is a good teacher') print('%s is a good teacher'%name) # 5 + 字串拼接 a='123' b='abc' c='789' d1=a+b+c print(d1) # +效率低,該用join d2=''.join([a,b,c]) print(d2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/59870.html
標籤:Python
上一篇:布爾型別
下一篇:運算子
