1 用''或者""来定义字符串
2''或者""在字符串中出现,造成不匹配:
如: str1='Gloria's house'
str1="Gloria's house"
str2="Python Course for "beginners" "
str2='Python Course for "beginners" '3 多行文本用""" """或者''' '''
email="""
Hi,Doggy
This is my first Python class. I fancy you.
Hope you watch this.
Love from,
Meow
"""print(email)
索引
str="Python Course for Beginners"print(str[0])#结果:Pprint(str[1])#结果:yprint(str[-1])#结果:sprint(str[:])#结果:Python Course for Beginnersprint(str[0:])#结果:Python Course for Beginnersprint(str[:3])#结果:Pytprint(str[0:3])#结果:Pytprint(str[1:-1])#结果:ython Course for Beginner
another=str[1:5]#结果:ythoprint(another)
字符串连接和格式化字符串
first="John"
last="Smith"
message=first+" ["+last+"] is a coder."
msg=f'{first} [{last}] is a coder.'print(message)print(msg)# John [Smith] is a coder.