设为首页 加入收藏

TOP

字符串类型内置方法
2019-05-23 18:07:58 】 浏览:32
Tags:字符串 类型 内置 方法

字符串类型内置方法

  • 按索引取值
  • 切片
  • 长度len
  • 成员用算in\not in
  • 移除空白strip
  • 切分split
  • 循环for
  • lstrip\rstrip
  • lower upper
  • startswith endswith
  • rsplit
  • join
  • replace
  • isdigit
msg ='Helle John'
print(f'{msg[6]}')
print(f'{msg[3:7:4]}')
print(len(msg))
print(f"{'john' in msg}")
print(f"{msg.startswith('Helle')}")#endswith 末尾
print(f"{msg.strip('h')}")# 移除两端

msg_list = msg.split('e') #rsplit()右切割
print(f'{msg_list}')

for i in msg:
    print(i)

print(f"{msg.lstrip('h')}") #右移rstrip
print(f"{msg.lower()}")
print(f'{msg.upper()}')

jion()

lis = [1,2,'19']
print(f"':'.join(lis): {':'.join(lis)}")  # 报错,数字不可和字符串拼接
# str之join()
lis = ['nick', 'male', '19']

print(f"':'.join(lis): {':'.join(lis)}")
':'.join(lis): nick:male:19

replace()

name = 'nick shuai'

print(f"name.replace('shuai','handsome'): {name.replace('shuai','handsome')}")
name.replace('shuai','handsome'): nick handsome

lidigit()

salary = '111'
print(salary.isdigit())  # True

salary = '111.1'
print(salary.isdigit())  # False
True
False
  • 其他操作
    1. find|rfind|index|rindex|count
    2. center|ljust|rjust|zfill
    3. expandtabs #默认制表符为8
    4. captalize|swapcase|title
    5. is系列
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Python]有关pygame库中的flip和u.. 下一篇pandas库Series类型与基本操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目