前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >字符串内置方法

字符串内置方法

作者头像
GH
发布2022-05-09 14:16:50
2470
发布2022-05-09 14:16:50
举报

目录

字符串内置方法

使用方法

1.索引

代码语言:javascript
复制
s = 'hello'
print(s[1])

e

2.索引切片

代码语言:javascript
复制
s='hello how are you'
print(s[0:4])#顾头不顾尾
print(s[0:4:2])#2表示步长,隔一个取一个
print(1,s[4:0:-1])# +从左到右,-表示从右到左
print(1,s[2:])# 左边的不写取到最左边,右边的不写取到最右边

hell hl 1 olle 1 llo how are you

3.for循环

代码语言:javascript
复制
for i in s:
    print(4,i)

4 h 4 e 4 l 4 l 4 o 4 4 h 4 o 4 w 4 4 a 4 r 4 e 4 4 y 4 o 4 u

4.成员运算

代码语言:javascript
复制
s='hello how are you'
print('hello' in s)
print('hello1' not in s)

True True

5.strip()

默认除去两端空格,可以指定去除的字符,可以指定多个字符同时去掉

代码语言:javascript
复制
s1 = '   juary     ***'
print(s1.strip(' j*'))

uary

6.split() 切割

代码语言:javascript
复制
s1 = 'hello|how|are|you'
print(s1.split('|'))# 按照|切割字符串,得到的是一个列表

['hello', 'how', 'are', 'you']

7. len 长度

代码语言:javascript
复制
s='hello how are you'
print(len(s))

17

8. lstrip()&rstrip()

代码语言:javascript
复制
s2='**hello**'
print(s2.lstrip('*'))#去掉左边的'*'
print(s2.rstrip('*'))#去掉右边的'*'

hello** **hello

9. lower&upper

代码语言:javascript
复制
s3='xiaomei'
print(s3.lower())#小写
print(s3.upper())#大写

xiaomei XIAOMEI

10. startswith&endswith

代码语言:javascript
复制
s='hello how are you'
print(s.startswith('hell'))#以。。。开始
print(s.endswith('you'))#以。。。结束,如果是则返回True,否则返回False

True True

11. rsplit

代码语言:javascript
复制
s1 = 'hello|how|are|you'
print(s1.split('|',1))#从左切割
print(s1.rsplit('|',1))#从右切割

['hello', 'how|are|you'] ['hello|how|are', 'you']

12. join

Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

代码语言:javascript
复制
s1 = 'hello|how|are|you'
lt = s1.split('|')
print(lt)
print('*'.join(lt))#使用*拼接列表内的每一个元素

['hello', 'how', 'are', 'you'] hello*how*are*you

13. replace

代码语言:javascript
复制
s='hello how are you'
s=s.replace('hello','hai')
print(s)

hai how are you

14. isdight&isalpha

代码语言:javascript
复制
s='123456s'
print(s.isdigit())#判断字符串内字符是否都为数字,返回bool值
print(s.isalpha())#判断字符串内字符是否都为字母,返回bool值

False False

15. find rfind index rindex count

代码语言:javascript
复制
s='hello how are you'
print(s.find('h'))#查找索引,没有找到返回-1
print(s.rfind('h',4,10))#(print(s,rfind))#(4,10)为在索引从(4,10)索引内查找

s5 = 'aaasssaaa'
print(s5.count('a'))#统计a的个数

0 6 6

16. center ljust rjust zfill

代码语言:javascript
复制
s = 'hai'
print(s.center(50,'*'))#居中
print(s.ljust(50,'*'))#居左
print(s.rjust(50,'*'))#居右
print(s.zfill(9))#往字符串内左侧填充0,填充到一共9个字符

hai* hai*********************************************** ***********************************************hai 000000hai

\n 换行 \t 缩进 expandtabs把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 8

代码语言:javascript
复制
S = "this is\tstring example....wow!!!"
 
print ("原始字符串: " + S)
print ("替换 \\t 符号: " +  S.expandtabs())
print ("使用16个空格替换 \\t 符号: " +  S.expandtabs(16))

原始字符串: this is string example....wow!!! 替换 \t 符号: this is string example....wow!!! 使用16个空格替换 \t 符号: this is string example....wow!!!

17. captalize swapcase title

代码语言:javascript
复制
s='hello how are you'
print(s.capitalize())#首字母大写
print(s.swapcase())#所有字母小写变大写,大写变小写
print(s.title())#每个单词的首字母大写

Hello how are you HELLO HOW ARE YOU Hello How Are You

​ 有序 or 无序:有序(有索引则有序) ​ 可变 or 不可变:不可变(值变id不变为可变,值变id也变为不可变)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字符串内置方法
    • 1.索引
      • 2.索引切片
        • 3.for循环
          • 4.成员运算
            • 5.strip()
              • 6.split() 切割
                • 7. len 长度
                  • 8. lstrip()&rstrip()
                    • 9. lower&upper
                      • 10. startswith&endswith
                        • 11. rsplit
                          • 12. join
                            • 13. replace
                              • 14. isdight&isalpha
                                • 15. find rfind index rindex count
                                  • 16. center ljust rjust zfill
                                    • 17. captalize swapcase title
                                    领券
                                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档