5.a字符串类型内置方法

字符串类型内置方法

优先掌握(*****)

  1. 按索引取值
  2. 切片
  3. 长度len
  4. 成员运算in|not in
  5. 移除空白strip
  6. 切分split
  7. 循环

1.按索引取值(只可取不可改变)

# str索引取值
msg = 'hello nick'
#      0123456789  # 索引序号

print(f'索引为6: {msg[6]}')
print(f'索引为-3: {msg[-3]}')
索引为6: n
索引为-3: i

2.切片(顾头不顾尾,步长)

# 索引切片
msg = 'hello nick'
#      0123456789  # 索引序号

print(f'切片3-最后: {msg[3:]}')
print(f'切片3-8: {msg[3:8]}')
print(f'切片3-8,步长为2: {msg[3:8:2]}')
print(f'切片3-最后,步长为2: {msg[3::2]}')

# 了解,步长为正从左到右;步长为负从右到左
print('\n**了解知识点**')
print(f'切片所有: {msg[:]}')
print(f'反转所有: {msg[::-1]}')
print(f'切片-5--2: {msg[-5:-2:1]}')
print(f'切片-2--5: {msg[-2:-5:-1]}')
切片3-最后: lo nick
切片3-8: lo ni
切片3-8,步长为2: l i
切片3-最后,步长为2: l ik

**了解知识点**
切片所有: hello nick
反转所有: kcin olleh
切片-5--2:  ni
切片-2--5: cin

3.长度len

# str长度
msg = 'hello nick'

print(len(msg))
10

4.成员运算in和not in

# str成员运算
msg = 'my name is nick, nick handsome'

print(f"'nick' in msg: {'nick' in msg}")
print(f"'jason' not in msg: {'jason' not in msg}")
print(f"not 'jason' in msg: {not 'jason' in msg}")
'nick' in msg: True
'jason' not in msg: True
not 'jason' in msg: True

5.移除空白strip()

# str移除空白strip()
name = '&&&n ick'

print(f"name.strip('&'): {name.strip('&')}")  # strip()默认为‘ ’,并且不修改原值,新创建空间
print(f"name: {name}")

# strip()应用场景
pwd = input('password: ')  # 用户可能会手抖输入空格
if pwd.strip() == '123':
    print('密码输入成功')

print(f"'*-& nick+'.strip('*-& +'): {'*-& nick+'.strip('*-& +')}")
name.strip('&'): n ick
name: &&&n ick
password: 123   
密码输入成功
'*-& nick+'.strip('*-& +'): nick

6.切分split

# str切分split
info = 'nick:male:19'
info_list1 = info.split(':')
info_list2 = info.split(':', 1)

print(f'info_list1:{info_list1}')
print(f'info_list2:{info_list2}')
info_list1:['nick', 'male', '19']
info_list2:['nick', 'male:19']

7.循环

msg = 'hello nick'

for i in msg:
    print(i)
h
e
l
l
o
 
n
i
c
k

需要掌握(****)

  1. lstrip&rstrip
  2. lower&upper
  3. startswith&endswith
  4. rsplit
  5. join
  6. replace
  7. isdigit

1.lstrip()和rstrip()

# str之lstrip()和rstrip()
name = '&&nick&&'

print(f"nick.lstrip('&'): {name.lstrip('&')}")
print(f"nick.rstrip('&'): {name.rstrip('&')}")
nick.lstrip('&'): nick&&
nick.rstrip('&'): &&nick

2.lower()和upper()

# str之lower()和upper()
name = 'Nick Chen'

print(f"name.upper(): {name.lower()}")
print(f"name.upper(): {name.upper()}")
name.upper(): nick chen
name.upper(): NICK CHEN

3.startswith()和endswith()

# str之startswith()和endswith()
name = 'Nick Chen'

print(f"name.startswith('Nick'): {name.startswith('Nick')}")
print(f"name.endswith('chen'): {name.endswith('chen')}")
name.startswith('Nick'): True
name.endswith('chen'): False

4.rsplit()

# str之rsplit()
info = 'nick:male:19'

print(f"info.rsplit(':', 1): {info.rsplit(':', 1)}")  # 从右开始切割
info.rsplit(':', 1): ['nick:male', '19']

5.join()

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

6.replace()

# str值replace()
name = 'nick shuai'

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

7.isdigit()

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

salary = '111.1'
print(salary.isdigit())  # False
True
False
# str之isdigit()应用场景
age = input('age: ')
if age.isdigit():
    age = int(age)

    if age < 18:
        print('小姐姐')
    else:
        print('阿姨好')
else:
    print(f'你的年龄能是这个{age}?')
age: 逗你玩?
你的年龄能是这个逗你玩??

其他操作(**)

  1. find|rfind|index|rindex|count
  2. center|ljust|rjust|zfill
  3. expandtabs
  4. captalize|swapcase|title
  5. is系列

1.find()、rfind()、index()、rindex()、count()

# str之find()、rfind()、index()、rindex()、count()
msg = 'my name is tank, tank shi sb, hha'

print(f"msg.find('tank'): {msg.find('tank')}")  # 找不到返回-1
print(f"msg.find('tank',0,3): {msg.find('tank',0,3)}")
print(f"msg.rfind('tank'): {msg.rfind('tank')}")  # 找不到返回-1
print(f"msg.index('tank'): {msg.index('tank')}")  # 找不到报错
print(f"msg.rindex('tank'): {msg.rindex('tank')}")  # 找不到报错
      

print(f"msg.count('tank'): {msg.count('tank')}")
msg.find('tank'): 11
msg.find('tank',0,3): -1
msg.rfind('tank'): 17
msg.index('tank'): 11
msg.rindex('tank'): 17
msg.count('tank'): 2

2.center()、ljust()、rjust()、zfill()

# str之center()、ljust()、rjust()、zfill()
print(f"'info nick'.center(50,'*'): {'info nick'.center(50,'*')}")
print(f"'info nick'.ljust(50,'*'): {'info nick'.ljust(50,'*')}")
print(f"'info nick'.rjust(50,'*'): {'info nick'.rjust(50,'*')}")
print(f"'info nick'.zfill(50): {'info nick'.zfill(50)}")  # 默认用0填充
'info nick'.center(50,'*'): ********************info nick*********************
'info nick'.ljust(50,'*'): info nick*****************************************
'info nick'.rjust(50,'*'): *****************************************info nick
'info nick'.zfill(50): 00000000000000000000000000000000000000000info nick

3.expandtabs()

# str之expandtabs()
print("a\\tb\\tc: %s"%('a\tb\tc\t'))  # 默认制表符8个空格
print("'a\\tb\\tc'.expandtabs(32): %s"%('a\tb\tc\t'.expandtabs(32)))
a\tb\tc: a  b   c   
'a\tb\tc'.expandtabs(32): a                               b                               c                               

4.captalize()、swapcase()、title()

# str之captalize()、swapcase()、title()
name = 'nick handsome sWAPCASE'

print(f"name.capitalize(): {name.capitalize()}")
print(f"name.swapcase(): {name.swapcase()}")  # 大小写互转
print(f"name.title(): {name.title()}")
name.capitalize(): Nick handsome swapcase
name.swapcase(): NICK HANDSOME Swapcase
name.title(): Nick Handsome Swapcase
posted @ 2019-05-08 15:18  yellowcloud  阅读(136)  评论(0编辑  收藏  举报
回顶部