字符串
1. 字符串
由有限的字符组成的有限序列。表述如下:str=“helle,world” str1=‘hello,world’
由单引号或双引号围起来的单个或多个字符表示一个字符串。
2. 转义字符
反斜杠\ +字符 组合表示其他含义
- \n 表示换行,
- \t表示制表符
- 要在字符串中表示单引号或双引号 ’ 或" .
- 同样 要在字符串中表示 "\ “就要用”\"来表示。
- 如果不希望字符串中的
\
表示转义,我们可以通过在字符串的最前面加上字母r
来加以说明:
str="you ask:\"where are you from?\""
str1 = r'\"hello, world!\"'
str2 = r'\t\\hello, world!\\\t'
print(str) #`输出显示 you ask:"where are you from?"
print(str1, str2) #\"hello, world!\" \t\\hello, world!\\\t
3. 字符串运算
- 用+号来表示两个字符串拼接
str=‘hello’
str1=‘world’
str2=str+’ '+str1
print(str2) #输出结果:hello world - 判断一个字符串是否包含另一个字符串包含 ‘in’或者‘not in’
str="hello boy hello girl hello dear"
str1="hello bor"
str3=hello boy"
print(str1 in str)