第七次python笔记

GB2312是我国制定的中文编码,使用1个字节表示英语,2个字节表示中文;GBK是GB2312的扩充,而CP936是微软在GBK基础上开发的编码方式。GB2312、GBK和CP936都是使用2个字节表示中文。

UTF-8对全世界所有国家需要用到的字符进行了编码,以1个字节表示英语字符(兼容ASCII),以3个字节表示中文,还有些语言的符号使用2个字节(例如俄语和希腊语符号)或4个字节。

在这里插入图片描述

>>> int('555')
555
>>> '%s'%[1, 2, 3]        #直接把对象转换成字符串
'[1, 2, 3]'
>>> str((1,2,3))          #直接把对象转换成字符串
'(1, 2, 3)'
>>> str([1,2,3])
'[1, 2, 3]'
>>> list(str([1, 2, 3]))  #字符串中的每个字符都成为列表的元素
['[', '1', ',', ' ', '2', ',', ' ', '3', ']']
>>> eval(str([1, 2, 3]))
[1, 2, 3]

1.使用format方法进行格式化

>>> print("The number {0:,} in hex is: {0:#x}, the number {1} in oct is {1:#o}".format(5555,55))
The number 5,555 in hex is: 0x15b3, the number 55 in oct is 0o67

2.字符串连接join()

>>> li = ["apple", "peach", "banana", "pear"]
>>> ','.join(li)
'apple,peach,banana,pear'
>>> '.'.join(li)
'apple.peach.banana.pear'
>>> '::'.join(li)
'apple::peach::banana::pear'

3.字符串常用方法

3.1split()和rsplit()方法允许指定最大分割次数。

>>> s = '\n\nhello\t\t world \n\n\n My name is Dong   '
>>> s.split(None, 1)
['hello', 'world \n\n\n My name is Dong   ']
>>> s.rsplit(None, 1)
['\n\nhello\t\t world \n\n\n My name is', 'Dong']
>>> s.split(None, 2)
['hello', 'world', 'My name is Dong   ']

3.2对于split()和rsplit()方法,如果不指定分隔符,则字符串中的任何空白符号(空格、换行符、制表符等)都将被认为是分隔符,把连续多个空白字符看作一个分隔符。

>>> s = 'hello world \n\n My name is Dong   '
>>> s.split()
['hello', 'world', 'My', 'name', 'is', 'Dong']
>>> s = '\n\nhello world \n\n\n My name is Dong   '
>>> s.split()
['hello', 'world', 'My', 'name', 'is', 'Dong']
>>> s = '\n\nhello\t\t world \n\n\n My name\t is Dong   '
>>> s.split()
['hello', 'world', 'My', 'name', 'is', 'Dong']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值