python基础---字符串类型(14)

1、定义

msg = 'hello'  # msg=str('msg')
print(type(msg))

2、类型转换

str可以把任意其他类型都转成字符串

res = str({'a': 1})
print(res, type(res))

3、使用:内置方法

3.1优先掌握

3.1.1按索引取值(正向取+反向取):只能取

msg = 'hello world'
# 正向取
print(msg[0])
print(msg[5])
# 反向取
print(msg[-1])

3.1.2切片

索引的拓展应用,从一个大的字符串拷贝出一个子字符串(顾头不顾尾,步长)

msg = 'hello world'
# 顾头不顾尾
res = msg[0:5] 
print(res)  # hello
# 步长
res = msg[0:5:2]  # 0 2 4
print(res)  # hlo

# 反向步长
res = msg[5:0:-1]
print(res)

res = msg[:]  # res=msg[0:11]
print(res)

res = msg[::-1]  # 把整个字符串倒过来
print(res)

3.1.3长度len

msg = 'hello world'
print(len(msg))

3.1.4成员运算in和not in

判断一个子字符串是否存在于一个大的字符串

print('zhoushun' in 'my name is zhoushun')
print('zhoushun' not in 'my name is zhoushun')
print(not 'zhoushun' in 'my name is zhoushun')  # 不推荐使用

3.1.5移除字符串左右两侧的符号strip

默认去掉的是空格

#默认去掉的是空格
msg = '    zhoushun   '
res = msg.strip()  # 默认去掉的空格
print(msg)  # 不改变原值
print(res)  # 是产生了新值

msg = '****zhoushun***'
print(msg.strip('*'))

# 了解:strip只去两边 不去中间
msg = '****zh**ou***'
print(msg.strip('*'))

msg = '##*&zhou))*@'
print(msg.strip('*)#@&'))

# 应用
inp_user = input('your name:').strip()
inp_pwd = input('your password:').strip()
if inp_user == 'zhoushun' and inp_pwd =='123':
    print('登录成功!')
else:
    print("账号密码错误")

3.1.6 切分split

把一个字符串按照某种分隔符进行切分,得到一个列表
默认分割符是空格:

# 默认分割符是空格
info = 'zhoushun 18 man'
res = info.split()
print(res)  # ['zhoushun', '18', 'man']

指定分割符:


# 指定分割符
info = 'zhoushun:18:man'
res = info.split(':')
print(res)  # ['zhoushun', '18', 'man']

指定分割次数:

# 指定分割次数(了解)
info = 'zhoushun:18:man'
res = info.split(':', 1)
print(res)  # ['zhoushun', '18:man']

3.1.7 循环

info = 'zhoushun : 18 : man'
for i in info:
    print(i)

3.2 需要掌握

3.2.1 strip, lstrip, rstrip

msg = '***zhoushun****'
print(msg.strip('*'))  # zhoushun
print(msg.lstrip('*'))  # zhoushun****
print(msg.rstrip('*'))  # ***zhoushun

3.2.2 lower,upper

msg = 'AAVbvdCDCC'
print(msg.lower())  # aavbvdcdcc
print(msg.upper())  # AAVBVDCDCC

3.2.3 startswith,endswith

startswith:判断是否以什么开头
endswith:判断是否以什么结尾

print('good good study, day day up'.startswith('good'))  # True
print('good good study, day day up'.endswith('good'))  # False

3.2.4 format

按照位置取

res = '我的名字是{} 我的年龄是{}'.format('zhoushun', 18)
print(res)
 res = '我的名字是{0}{0}{0}我的年龄是{1}{1}'.format('zhoushun', 18)
 print(res)

打破位置的限制
按照key=value传值

res = "my name is {name} my age is {age}".format(age=18, name='zhoushun')
print(res)

3.2.5 split, rsplit:将字符串切成列表

print(info.split(':', 1))  # ['zhoushun', '18:man']
print(info.rsplit(':', 1))  # ['zhoushun:18', 'man']

3.2.6 join:把字符串拼接成字符串

l = ['zhoushun', '18', 'man']
res = ':'.join(l)  # 按照某个分割符号,把元素全为字符串的列表拼接成一个大字符串
print(res)  # zhoushun:18:man

3.2.7 replace

msg = 'good good study, day day up'
print(msg.replace('day', 'DAY'))  # good good study, DAY DAY up
print(msg.replace('day', 'DAY', 1))  # good good study, DAY day up

3.2.8 isdigit

判断字符串是否由纯数字组成

print('123'.isdigit())  # True
print('12.3'.isdigit())  # Fales

应用

age = input('请输入您的年龄:').strip()
if age.isdigit():
    age = int(age)
    if age >18 :
        print('猜大了')
    elif age < 18 :
        print('猜小了')
    else:
        print('猜对了')
else:
    print('必须输入数字')

4.3 了解

4.3.1 find,rfind,index,rindex,count

找到的情况下返回起始索引:

msg = 'hello world ,'
# 找到返回起始索引
print(msg.find('e'))  # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('world'))
print(msg.index('e'))
print(msg.index('world'))

找不到:

print(msg.find('xxx'))  # 返回-1,代表找不到
print(msg.index('xxx'))  # 抛出异常,程序崩溃
msg = 'hello world ,'
print(msg.count('o'))

4.3.2 center,ljust,rjust,zfill

print('zhou'.center(20, '='))  # ========zhou========
print('zhou'.ljust(20, '='))  # zhou================
print('zhou'.rjust(20, '='))  # ================zhou
print('zhou'.zfill(10))  # 000000zhou

4.3.3 expandtabs

msg = 'hello\tworld'
print(msg.expandtabs(2))  # 设置制表符代表的空格数为2

4.3.4 captalize,swapcase,title

print('hello world zhoushun'.capitalize())  # 第一个首字母大写 Hello world zhoushun
print('Hello World Zhoushun'.swapcase())  # 大小写反转   hELLO wORLD zHOUSHUN
print('hello world zhoushun'.title())  # 每个单词的首字母大写 Hello World Zhoushun

4.3.5 is

print('abc'.islower())
print('ACV'.isupper())
print('Hello Worlf'.istitle())
print('gegw23'.isalnum())   # 字符串由字母或数字组成结果为True
print('acv'.isalpha())  # 字符串由字母组成结果为True
print('   '.isspace())  # 字符串由空格组成结果为True
print('if'.isidentifier())
num1 = b'4'  # bytes
num2 = u'4'  # unicode,python3中无需加u就是unicode
num3 = '四'  # 中文数字
num4 = 'Ⅳ'  # 罗马数字

# isdigit 只能识别:num1,num2
print(num1.isdigit())  # True
print(num2.isdigit())  # True
print(num3.isdigit())  # False
print(num4.isdigit())  # False

# 4.3.7 isnumberic可以识别:num2,num3,num4
print(num2.isnumeric())  # True
print(num3.isnumeric())  # True
print(num4.isnumeric())  # True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值