字符串类型内置方法

字符串类型内置方法

  • 按索引取值
  • 切片
  • 长度len
  • 成员用算in ot in
  • 移除空白strip
  • 切分split
  • 循环for
  • lstrip strip
  • 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系列
原文地址:https://www.cnblogs.com/1naonao/p/10912691.html