Python学习——Python基本数据类型(一)

                                              python3 基本数据类型   
Python3 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。
Python3 中有6个标准的数据类型:Number(数字);字符串(String);列表(list);元组(Tuple);字典:(Dict);集合(Sets)

Number(数字):
Python3支持int,float,bool,complex(复数)
type()函数可以查看变量所指的对象类型

String(字符串):
Python中的字符串用单引号(')或双引号(")括起来,同时使用反斜杠(\)转义特殊字符。
注意:'''...'''三元引号在创建短字符串时没有什么特别用处,主要用于创建多行字符串,如下例:
>>> poem =  '''There was a Young Lady of Norway,
... Who casually sat in a doorway;
... When the door squeezed her flat,
... She exclaimed, "What of that?"
... This courageous Young Lady of Norway.'''

Python也允许空串存在,不包含任何字符,完全合法!
数字与字符串之间的转换:

字符串转换成数字:
>>> int('20')
20
>>> float('20')
20.0
>>> int(20)
20

数字转换成字符串:
>>> str(20)
'20'
>>> str(20.0)
'20.0'
>>> str(True)
'True'

使用+拼接
在python中可以试用+将多个字符串或字符串变量进行拼接
>>> 'Release the Tom!' + 'At once!'
Release the Tom! At once!

使用[]提取字符
在字符串后面添加[],在[]中添加偏移量,即可取出该位置的字符串。第一个字符偏移量为0,第二个为1,后面依次类推。 右边第一个偏移量为-1,第二个为-2,从右往左依次类推...
>>> str = 'abcdefghijklmnopqrstuvwxyz'
>>> str[0]
'a'
>>> str[-1]
'z'
>>> str[3]
'd'

字符串是不可变的,有时候改变字符串,需要组合使用一些字符串函数:replace(),以及分片操作
>>> name = 'Henny'
>>> name.replace('H','P')
'Penny'
>>> 'P' + name[1:]
'Penny'

使用[start:end:step]分片:
分片操作:可以从一个字符串中抽取子字符串。起始偏移量start,终止偏移量end以及可选的步长step来定义一个分片
1.[:]提取从开头到结尾的整个字符串
2.[start:]从start提取到结尾
3.[:end]从开头提取到end-1
4.[start:end]从start提取到end-1结尾
5.[start:end:step]从start提取到end-1,每个step提取一个字符

>>> str = 'qwertyuiop'
>>> str[:]
'qwertyuiop'
>>> str[5:]
'yuiop'
>>> str[-3:]
'iop'
>>> str[:-3]
'qwertyu'
>>> str[-6:-3]
'tyu'
步长为3
>>> str[::3]
'qrup'
利用切片反向输出字符串
>>> str[::-1]
'poiuytrewq'

字符串其他常用操作:
>>> str = 'qwertyuiop'

计算字符串的长度
>>> len(str)
10

使用split()分割:
使用内置的字符串函数 split() 可以基于 分隔符 将字符串分割成由若干子串组成的 列表 。
所谓列表(list)是由一系列值组成的序列,值与值之间由逗号隔开,整个列表被方括号所包裹。
>>> todos = 'get gloves,get mask,give cat vitamins,call ambulance'
>>> todos.split(',')
['get gloves', 'get mask', 'give cat vitamins', 'call ambulance']
上面例子中,字符串名为 todos,函数名为 split(),传入的参数为单一的分隔符split(),传入的参数为单一的分隔符 ','。
如果不指定分隔符,那么 split() 将默认使用空白字符——换行符、空格、制表符。
>>> todos.split()
['get', 'gloves,get', 'mask,give', 'cat', 'vitamins,call', 'ambulance']

使用join()合并:
可能你已经猜到了,join() 函数与 split() 函数正好相反:它将包含若干子串的列表分解,并将这些子串合成一个完整的大的字符串。join()
>>> crypto_list = ['Yeti', 'Bigfoot', 'Loch Ness Monster']
>>> crypto_string = ', '.join(crypto_list)
>>> print('Found and signing book deals:', crypto_string)
Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster

使用replace()替换:
>>> str = 'qwertyuiop'
>>> str.replace('w','ooooo')
'qoooooertyuiop'
最多修改3处
>>> str = 'qwerwerwerwtytewwiitw'
>>> str.replace('w','oooo',3)
'qooooerooooerooooerwtytewwiitw'
计算字符串中'w'出现的次数
>>> str.count('w')
7

转载于:https://www.cnblogs.com/huan-ge/p/6624663.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值