python字符串操作方法

╰半夏微凉° 2023-07-19 12:29 72阅读 0赞

一、hello world

hello

o

world

  1. s1 = 'hello world'
  2. s3='''
  3. hell
  4. o
  5. world
  6. '''
  7. print(s1,s3,end='')

二、 ‘hello world!’

\hello,world!\

  1. s1 = '\'hello world!\''
  2. s2 = '\n\\hello,world!\\\n'
  3. print(s1,s2,end='')

三、\‘hello,world!\‘ \n\\hello,world!\\\n

  1. s1 = r'\'hello,world!\''
  2. s2 = r'\n\\hello,world!\\\n'
  3. print(s1,s2,end='')

四、hellohellohello

  1. s1 = 'hello' * 3
  2. print(s1)

五、hellohellohelloworld

  1. s1 = 'hello' * 3
  2. s2='world'
  3. s1 += s2
  4. print(s1)

六、True

  1. s1 = 'hello' * 3
  2. s2='world'
  3. s1 += s2
  4. print('ll' in s1)

七、False

  1. s1 = 'hello' * 3
  2. s2='world'
  3. s1 += s2
  4. print('good' in s1) # False

八、字符串截取,python字符串截取的原则是前闭后开,即切取字符串为开始索引到结束索引-1内的字符串

如:print(str2[2:5]) #输出为c12。字符串截取的第三个参数是间隔数

20200331122015138.png

  1. str2 = 'abc123456'
  2. # 从字符串中取出指定位置的字符(下标运算)
  3. print(str2[2]) # c
  4. # 字符串切片(从指定的开始索引到指定的结束索引)
  5. print(str2[2:5]) # c12
  6. print(str2[2:]) # c123456
  7. print(str2[2::2]) # c246
  8. print(str2[::2]) # ac246
  9. print(str2[::-1]) # 654321cba
  10. print(str2[-1::-1] #654321cba
  11. print(str2[-3:-1]) # 45

发表评论

表情:
评论列表 (有 0 条评论,72人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python 字符串操作

    目录 字符串常用操作 1、去空格及特殊符号 2、复制字符串 3、连接字符串 4、查找字符 5、比较字符串 6、扫描字符串是否包含指定的字符 7、字符串长度 8