字符串
字符串带单引号只需要用双引号括起来 字符串带双引号只需要用单引号括起来即可
字符转义:\t水平制表符 \n换行符 \r回车 \"双引号 \'单引号 \\ 反斜线
原始字符串 在字符串前面加字母r 意思为raw string
s='Hello\n world'
s
'Hello\n world'
print(s)
Hello
world
s=r'Hello\n world'
s
'Hello\\n world'
print(s)
Hello\n world
注意,打印出来的字符串与字符串可能不同 因为存在转义字符
长字符串需要用"""或'''括起来(三个单引号或三个双引号)
s='''beautiful morning
no
yes
oh'''
s
'beautiful morning\nno\nyes\noh'
print(s)
beautiful morning
no
yes
oh
字符串转化为数字 int("str",进制) 或float()
int("80")
80
int("80.0")
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
int("80.0")
ValueError: invalid literal for int() with base 10: '80.0'
#int方法转换浮点数出错
int("AB")
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
int("AB")
ValueError: invalid literal for int() with base 10: 'AB'
#int默认十进制转换AB出错
int("AB",16)
171
float("80.0")
80.0
数字转换为字符串 s_str()
字符串查找s.find(字符串,开始位置,结束位置)注意,查找内容不包括结束位置但包括开始位置,若查找范围内有多个匹配片段,则返回从左到右第一个位置,若查找不到匹配片段,则返回-1zi
字符串替换s.replacce(old,new,count)count为替换几个old字符串为新字符串,若未指明则全部替换
字符串分割s.split(字符串,maxsplit=x) maxsplit为最大分割次数,不指明则不限制次数