Python数字类型及字符串类型的内置方法 ##
数字类型及字符串类型的内置方法
数字类型
数字类型有哪些?
整型和浮点型
有哪些运算符?
平时用的加减乘除、整除、模运算、幂运算,比较运算等
他们的属性?
数字类型没有有序,无序一说。换言之,不存在索引这个概念
数字类型属于不可变数据类型。换言之,如果值发生变化,内存地址也会变化
字符串类型(重点)
一些特殊定义:
r'原生字符串,里面的\n没有换行的意思'
b'010101010101' # 二进制编码的字符串
u'unicode' # unicode编码的字符串
用索引取值
name = 'i am an apple.'
print(name[0])
print(name[1])
print(name[10])
name[1] = 'b' #这行代码会报错,记住,按索引取值,只能取,不能赋值
切片取值
hello = 'hello world!'
print(hello[3:]) # 从索引3切到最后
print(hello[3:5]) # 从索引3切到索引5,包括索引3,但不包括索引5,俗称“顾头不顾尾”
print(hello[3:10:2]) # 从索引3切到索引10,补偿为2,即每次跳2个索引
print(hello[:]) # 切了全部内容,相当于什么也没变化
print(hello[::-1]) # 反转切片。步长-1,表示倒叙切片
print(hello[-2:-5:1]) # 思考下,为什么这切片,切出来的东西是空的
print(hello[-2:-5:-1]) # 从-2反向切到-5,但不包括-5的值
len方法
hello = 'hello world!'
apple = 'i like apple really.'
question = 'Do you know how long is my height?'
print(len(hello))
print(len(apple))
print(len(question))
# it can show your length instant
in & not in
hello = 'hello world!'
apple = 'i like apple really.'
print('hello' in hello) # 判断变量hello里是否有‘hello’,如果有,返回True
print('apple' not in apple) # 判断变量apple是否“没有‘apple’”,如果没有返回True
strip lstrip rstrip
blank = ' i do not want so many blanks on my heads and tails. '
print(blank.strip()) # 清除左右两边的对应字符。若空,表示去除空格
print(blank.lstrip()) # 清除左边的空格,即头
print(blank.rstrip()) # 清除右边的空格,即尾
split rsplit
# split,通过指定字符把字符串切换成列表(不包括原来的指定字符)
apple = 'iphone imac ipad iwatch macpro'
fake_news = 'cnn:new york times:fox news net'
print(apple.split()) # 没有指定字符串,则默认是空格,相当于默认是' '
print(fake_news.split(':')) # 指定了“:”,则用“;”来进行分割
# 自己查看下面2组代码运行结果的不同,体会下split和rsplit的区别
print(apple.split(' ', 2))
print(apple.rsplit(' ', 2))
循环
apple = 'iphone imac ipad iwatch macpro'
for i in apple:print(i)
# 使用for遍历字符串中所有字符
upper & lower
need_lower = 'I need be Lowercase.'
need_upper = 'i need be capital style.'
print(need_lower.lower())
print(need_upper.upper())
startswith and endswith
person = 'you and i'
print(person.startswith('you'))
print(person.endswith('i'))
# 判断字符串是否以指定字符开始或结尾。返回True或False
join
# join方法,把字符串插入一个字符串列表之间,返回新的字符串
apple_products_list = ['iphone', 'macbook', 'imac', 'ipad', 'iwatch']
print(':'.join(apple_products_list))
replace
teacher = 'Nick is shuai'
print(teacher.replace('shuai', 'handsome'))
转载于:https://www.cnblogs.com/heroknot/p/10912839.html
Python数字类型及字符串类型的内置方法 ##相关推荐
- 字符串的常用内置方法
字符串的常用内置方法 capitalize() 将字符串的第一个字符转换为大写. lower() 转换字符串中所有大写字符为小写. upper() 转换字符串中的小写字母为大写. swapcase() ...
- Python入门学习 DAY 05 字符串,列表的内置方法
Python入门 DAY 05 本篇文章主要介绍字符串类型及内置方法,列表类型及内置方法 1.字符串类型及内置方法 (1)字符串定义 1.字符串中的字符是有序的 可以通过 ...
- 内置python数据类型_python基础之数据类型及内置方法
一.数字类型 1.整型int:用来记录年龄.等级.号码等 int()可以接收数字和纯数字组成的字符串参数,不能接收浮点数的字符串. 当参数为浮点数时,函数会返回参数的整数部分.当参数是整数字符串时,函 ...
- 详细讲解Python字符串的各种内置方法
1.capitalize() 把字符串第一个字母变成大写; a='xiao' a.capitalize() 'Xiao' 2.casefold() 把所有字母变成小写 a='XiAo' a.casef ...
- python判断语句的复杂度_Python内置方法的时间复杂度(转)
本文翻译自Python Wiki 本文基于GPL v2协议,转载请保留此协议. 本页面涵盖了Python中若干方法的时间复杂度(或者叫"大欧","Big O") ...
- day 07总结(数字类型内置方法/字符串类型内置方法)
一.数字类型内置方法 整型和浮点型统称为数字类型. 1.整型内置方法(int) 作用:描述年龄.号码.等级等. 定义方式:可以使用int()方法将纯数字的字符串转为十进制的整型. 使用方法: age ...
- python的内置字典数据类型_python 数据类型元组与字典内置方法
1.元组 (1)元组是不可变的列表,能存多个值:如果多个值只有取得需求,没有改的需求,用元组最合理 (2)定义:在()内用逗号隔开,可以存任意类型的值 注意:当元组只有一个元素时,要在后面加逗号 # ...
- 序列内置方法详解(string/list/tuple)
一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...
- python的四种内置数字类型_浅析Python数字类型和字符串类型的内置方法
一.数字类型内置方法 1.1 整型的内置方法 作用 描述年龄.号码.id号 定义方式 x = 10 x = int('10') x = int(10.1) x = int('10.1') # 报错 内 ...
- python内置类型方法_浅析Python数字类型和字符串类型的内置方法
一.数字类型内置方法 1.1 整型的内置方法 作用 描述年龄.号码.id号 定义方式 x = 10 x = int('10') x = int(10.1) x = int('10.1') # 报错 内 ...
最新文章
- 【spring 5】AOP:spring中对于AOP的的实现
- SQL学习笔记 where子句用法,like关键字 嵌套查询
- 洛谷 1541 乌龟棋
- 【Spring MVC】文件上传、文件下载
- 【HDU - 1754】I Hate It (线段树模板 单点覆盖更新+区间最大值查询)
- visual studio 高级选项及配置
- C# 随机数生成避免重复
- 面试没过的程序员都到哪去了?
- 50.远程连接服务器 SSH, XDMCP, VNC, XRDP
- 总数量超过五十个,史上最全的爬虫项目集合
- NSA武器库知识整理
- mysql dump hbase_mysqldump 导出部分数据库
- [翻译] 数学翻译词汇
- 史上最全软件测试工程师常见的面试题总结(四)【多测师_王sir】
- 股票自动交易接口简化
- Transformers for 1D signals in Parkinson’s disease detection from gait
- 声音的三要素跟声波的幅度、频率和频谱的对应关系
- The song from the Chef
- Stackoverflow的用法
- 网银互联获杭州市云计算协会理事单位授牌