python学习日记14——字符串

1.字符串查找

# index()查找字符串第一次出现的位置,没有就报错
# rindex()查找字符串第一次出现的位置,没有就报错
# find()查找字符串最后一次出现的位置,没有返回-1
# rfind()查找字符串最后一次出现的位置,没有返回-1
s = 'hello,hello'
print(s.index('lo'))
print(s.rindex('lo'))
print(s.find('lo'))
print(s.rfind('lo'))

2.大小写转换

# upper()全部转换成大写
a = 'hello,world'.upper()
print(a)
# lower()全部转换为小写
b = 'HELLO,WORLD'.lower()
print(b)
# swapcase()大写转小写,小写转大写
c = 'Hello,World'.swapcase()
print(c)
# title()每个单词的第一个字符变为大写,剩余字符变为小写
d = 'hello,world'.title()
print(d)
# capitalize第一个字符转换为大写,其余小写
e = 'hello,world'.capitalize()
print(e)

3.内容对齐

# center()居中对齐,第一个参数指定宽度,第二个指定填充,不填默认空格,宽度小于字符串宽度返回原字符串
print('hello,world'.center(20, '*'))
# ljust()左对齐,第一个参数指定宽度,第二个指定填充,不填默认空格,宽度小于字符串宽度返回原字符串
print('hello,world'.ljust(20,'*'))
# rjust()左对齐,第一个参数指定宽度,第二个指定填充,不填默认空格,宽度小于字符串宽度返回原字符串
print('hello,world'.rjust(20,'*'))
# zfill()右对齐,用0填充,只接收1个参数
print('hello,world'.zfill(20))
print('-980'.zfill(8))

4.字符串的劈分

# split()从左边开始分割,分隔符默认是空格,返回值是列表
s = 'hello world hello'
lst = s.split()
print(lst)
# sep用指定分割符
s = 'hello|world|hello'
print(s.split(sep='|'))
# maxsplit指定分割次数
print(s.split(sep='|', maxsplit=1))
# rsplit()从右边开始劈分,与split()类似

5.字符串判断方法

# isidentifier()判断字符串是不是合法标识符
print('hello,world'.isidentifier())  # False
print('_hello'.isidentifier())       # True
# isspace()判断字符串是否有空白字符组成(回车,换行,水平制表符)
print('\t'.isspace())                # True
# isalpha()判断字符串是否全部由字母组成
print('张三'.isalpha())               # True,不用管
print('abc123'.isalpha())            # False
# isdecimal()判断字符串是否全由十进制数字组成
print('1596四'.isdecimal())            # False
print('Ⅱ'.isdecimal())                # False
# isnumeric()判断字符串是否全由数字组成
print('1596四'.isnumeric())            # True
print('Ⅱ'.isnumeric())                # True
# isalnum()是否全由字母和数字组成
print('12asd'.isalnum())               # True
print('张三123'.isalnum())              # True

6.替换与合并

''' 替换 '''
s = 'hello,python'
print(s.replace('python','java'))            # hello,java
s1 = 'hello,python,python,python'
print(s1.replace('python','java',2))  # hello,java,java,python

''' 合并 (列表,元组,字符串) '''
lst = ['hello', 'world', 'world']
print(','.join(lst))           # hello,world,world
s2 = ('python', 'hello', 'world')
print(','.join(s2))            # python,hello,world

print(','.join('hello'))      # h,e,l,l,o

7.字符串的比较

# 字符比较,就是原始值比较
# 字符串比较,从左往右一个字符一个字符的比较,直到第一个不相等的字符,其结果就是最终结果
print('apple' > 'app')     # True
print('apple' > 'banana')  # False

# 原始值与字符
print(ord('a'), ord('b'))
print(chr(97), chr(98))

print(ord('赵'))
print(chr(36213))

8.字符串的切片

 字符串不可变,切片产生新的对象

s = 'hello,world'
s1 = s[:5]
s2 = s[6:]
s3 = s1+'!'+s2
print(s1)  # hello
print(s2)  # world
print(s3)  # hello!world

print(s[1:5:2])  # el
print(s[::-1])   # dlrow,olleh

9.格式化字符串

# %做占位符
name = '张三'
age = 30
print('我叫%s,今年%d岁了' % (name, age))  # 我叫张三,今年30岁了

# {}做占位符
print('我叫{0},今年{1}岁了,真的是{1}了'.format(name, age))  # 我叫张三,今年30岁了,真的是30了

# f-string
print(f'我叫{name},今年{age}了')  # 我叫张三,今年30了

# 宽度
print('%10d' % 99)
# 保留小数
print('%.3f' % 3.1415926)
# 同时使用
print('%10.3f' % 3.14215926)
'''
        99
3.142
     3.142
'''

# 第二种宽度和精度
print('{0:.3}'.format(3.1415926))    # .3表示一共3位
print('{0:.3f}'.format(3.1415926))   # .3f表示3位小数
print('{0:10.3f}'.format(3.1415926))
''''
3.14
3.142
     3.142
'''

10.编码与解码

# 编码
s = '天涯共此时'
print(s.encode(encoding='GBK'))    # GBK中一个中文占两个字节
print(s.encode(encoding='UTF-8'))  # UTF-8一个中文占三个字节
''''
b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'
'''
# 解码
print(b'\xcc\xec'.decode(encoding='GBK'))  # 天

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值