一、引号
1.单行文本:'单引号'和"双引号"
2.多行段落文本:'''三引号'''
print('''三引号实现
多行段落文本''')
二、转义字符
需要在字符串中使用特殊字符时,用反斜杠"\"转义字符来表示
三、字符串的索引:str[num]
word='abcdefg'
print(word[1])
#b
四、字符串的切片:str[start:end]
规则:左取右不取
返回从start到end(-1)以step为步长的字符串
word = 'abcdefg'
print(word[1:6:2])
# 返回 bdf
五、字符串的连接:str1+str2
使用加号连接两个字符串
print('字符串1'+'字符串2')
#返回 字符串1字符串2
六、字符串的处理方法
(一)字符串的分隔
str.split(sep=None)
返回由str根据sep被分隔的部分组成的列表,默认以空格分隔
words = '小明,小红,小黄,小绿'
print(words.split(','))
# 返回 ['小明','小红','小黄','小绿']
(二)字符串的替换
str.replace(old,new)
所有的old字符串替换为new字符串
word = 'pythonpy'
print(word.replace('py','PY'))
# 返回 PYthonPY
(三)字符串中的部分字符的删除
str.strip(chars)
从str中删除掉在其左右的chars字符
word = 'python'
print(word.strip('py'))
# 返回 thon
(四)字符串中字符的添加
str.join(iter)
除最后,在iter的每个元素后面添加一个str
word = 'python'
print(','.join(word))
# 返回 p,y,t,h,o,n