python字符串操作方法
一、hello world
hello
o
world
s1 = 'hello world'
s3='''
hell
o
world
'''
print(s1,s3,end='')
二、 ‘hello world!’
\hello,world!\
s1 = '\'hello world!\''
s2 = '\n\\hello,world!\\\n'
print(s1,s2,end='')
三、\‘hello,world!\‘ \n\\hello,world!\\\n
s1 = r'\'hello,world!\''
s2 = r'\n\\hello,world!\\\n'
print(s1,s2,end='')
四、hellohellohello
s1 = 'hello' * 3
print(s1)
五、hellohellohelloworld
s1 = 'hello' * 3
s2='world'
s1 += s2
print(s1)
六、True
s1 = 'hello' * 3
s2='world'
s1 += s2
print('ll' in s1)
七、False
s1 = 'hello' * 3
s2='world'
s1 += s2
print('good' in s1) # False
八、字符串截取,python字符串截取的原则是前闭后开,即切取字符串为开始索引到结束索引-1内的字符串
如:print(str2[2:5]) #输出为c12。字符串截取的第三个参数是间隔数
str2 = 'abc123456'
# 从字符串中取出指定位置的字符(下标运算)
print(str2[2]) # c
# 字符串切片(从指定的开始索引到指定的结束索引)
print(str2[2:5]) # c12
print(str2[2:]) # c123456
print(str2[2::2]) # c246
print(str2[::2]) # ac246
print(str2[::-1]) # 654321cba
print(str2[-1::-1] #654321cba
print(str2[-3:-1]) # 45
还没有评论,来说两句吧...