# 把前面的引号理解为起始符,后面的理解为终止符 # 单双引号的灵活运用 想输出"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)