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

python字符串内置方法[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-21 09:15:02
2440
发布2022-07-21 09:15:02
举报

大家好,又见面了,我是你们的朋友全栈君。

字符串的内置方法(部分)

代码语言:javascript
复制
    移除空白strip
# msg='             hello         '
# print(msg)
# print(msg.strip())

# msg='***hello*********'
# msg=msg.strip('*')
# print(msg)

# print(msg.lstrip('*'))
# print(msg.rstrip('*'))

#用处
# while True:
#     name=input('user: ').strip()
#     password=input('password: ').strip()
#
#     if name == 'egon' and password == '123':
#         print('login successfull')



#     切分split
# info='root:x:0:0::/root:/bin/bash'
# print(info[0]+info[1]+info[2]+info[3])

# user_l=info.split(':')
# print(user_l[0])

# msg='hello world egon say hahah'
# print(msg.split()) #默认以空格作为分隔符

cmd='download|xhp.mov|3000'
# cmd_l=cmd.split('|')
# print(cmd_l[1])
# print(cmd_l[0])

# print(cmd.split('|',1))

#用处
# while True:
#     cmd=input('>>: ').strip()
#     if len(cmd) == 0:continue
#     cmd_l=cmd.split()
#     print('命令是:%s 命令的参数是:%s' %(cmd_l[0],cmd_l[1]))




#
#     长度len

# print(len('hell 123'))

#
#     索引

#
#     切片:切出子字符串
# msg='hello world'
# print(msg[1:3]) #1 2
# print(msg[1:4]) #1 2 3




# oldboy_age=84
# while True:
#     age=input('>>: ').strip()
#     if len(age) == 0:continue
#     if age.isdigit():
#         age=int(age)
#     else:
#         print('must be int')





#startswith,endswith
# name='alex_SB'
# print(name.endswith('SB'))
# print(name.startswith('alex'))


#replace
# name='alex say :i have one tesla,my name is alex'
# print(name.replace('alex','SB',1))

# print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))
# print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
# print('my name is {0} my age is {1} my sex is {0}:{2}'.format('egon',18,'male'))
# print('my name is {name} my age is {age} my sex is {sex}'.format(
#     sex='male',
#     age=18,
#     name='egon'))


# name='goee say hello'
# # print(name.find('S',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# # print(name.index('S')) #同上,但是找不到会报错
#
# print(name.count('S',1,5)) #顾头不顾尾,如果不指定范围则查找所有


#join
# info='root:x:0:0::/root:/bin/bash'
# print(info.split(':'))

# l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# print(':'.join(l))


#lower,upper
# name='eGon'
# print(name.lower())
# print(name.upper())

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124541.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年3月2,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字符串的内置方法(部分)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档