開始跟着視頻學python,第二天mark【字符串拼接】

今天第二天,從第30分鐘開始看。

course = 'python for beginners'
course2 = "python's for beginners"   #爲了python's  前後都用了雙引號
course3 = 'python for "beginners"' 
print(course)
print(course2)
print(course3)

注意上面第二句爲了python’s 前後都用了雙引號。

如果想輸入很多話的話可以用‘’‘

course = '''
hi moon 
here an email for you
i love you
bye
'''
print(course)

索引

course = 'python for beginners'
#         01234567
print(course[0])    #第一個,p
print(course[6])   #空格
print(course[0:3])    # 是0,1,2 (pyt) 沒有h  額
print(course[0:])
print(course[3:])   #從3開始,h
print(course[:5])   #01234都有 ,pytho
another = course[:]   #複製了
another1 = course   #這個也行
print(another)
print(another1)

輸出爲

p
 
pyt
python for beginners
hon for beginners
pytho
python for beginners
python for beginners

問題來了,如果

name = 'jennifer'
print(name[1:-2])

會輸出什麼呢?
我猜的是ejr,錯了。答案是:

ennif

這個

name = 'jennifer'
print(name[1:0])
print(name[2:0])   #都沒有輸出結果

拼接

 first = 'jhon'
last = 'smith'
message = first + '[' + last + ']  is  a coder'
print(message)

輸出:

jhon[smith]  is  a coder

上面這種不夠直觀,讀代碼時。
另一種方式如下

first = 'jhon'
last = 'smith'
message = first + '[' + last + ']  is  a coder'
msg = f'{first}[{last}] is a code'    #這裏用了一個f'',format(格式)  {}裏面的內容被替換了
print(message)
print(msg)

輸出如下

jhon[smith]  is  a coder
jhon [smith] is a code

小結:利用f,和{},在字符串中動態地插入值。
mark一下,視頻位置:41:00
python教程2019版 6小時完全入門 並且達到能開發網站的能力 目前最好的python教程 (含中文翻譯)_嗶哩嗶哩 (゜-゜)つロ 乾杯~-bilibili

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章