# 把前面的引號理解為起始符,后面的理解為終止符 # 單雙引號的靈活運用 想輸出"hello,Q" 用單引號 # 想輸出 This is Q's 用雙引號 # 想輸出既有單引號又有雙引號或者特定格式 用三對單引號'''###''' word = '"hello,Q"' word2 = "This is Q's" word3 = ''' Hi,Q!your babe always said "This is Q's". Do you have spare time? We should have a talk.Thank you, yours Tom ''' # index獲取,從0開始012345....或者-6-5-4-3-2-1 print(word[7]) print(word[-2]) # 提取一段 左閉右開 print(word[0:3]) #如果沒有值會默認填寫 min:max print(word[1:]) print(word[:5]) #字符串復制 new_word=word[1:6] print(new_word) print(word2) print(word3)
猜猜下面的代碼的輸出是什么?
# index的正負應用
name = "Jennifer"
print(name[1:-1])
字符串多個方法的應用
# 組合str輸出
first = 'Stella'
last = 'Smith'
message = first + ' [' + last + ']' + ' is a coder'
msg = f'{first} [{last}] is a coder'
print(message)
print(msg)# 字符串計算 都是不會改變原來字符串的
course = 'Python for Beginners'
print(len(course))
# 在不改變原來字符串的基礎上,全部變為大寫/小寫/首字母大寫
print(course.upper())
print(course.lower())
print(course.title())
#返回第一次查找成功的index 未找到返回-1 查找區分大小寫
print(course.find('n'))
# 替換 多個/一個 替換一個/多個
print(course.replace('Py','stella'))
#查找字符串里有沒有相應子串 返回布爾類型
print('Python' in course)print(course)