python中三种基本的数值类型(python基础知识一变量与简单数据类型详解)
类别:脚本大全 浏览量:1276
时间:2021-10-14 00:51:45 python中三种基本的数值类型
python基础知识一变量与简单数据类型详解1.1变量
变量的命名规则:
- 1、只能包含字母、数字、下划线,且不能用数字开头
- 2、不能使用python关键字
- 3、简短且具有描述性
1.2字符串
python中用引号引起来的都是字符串,单引号双引号都可以
a.字符串的索引
|
s = 'ABCDEF' #索引 s1 = s[ 0 ] s2 = s[ - 1 ] s3 = s[ 0 : 4 ] print (s1) #A print (s2) #F print (s3) #ABC,左闭右开 #打印全部 s4 = s[:] #s[0:] print (s4) s5 = s[ 0 : 5 : 2 ] print (s5) #ACE s6 = s[ 3 :: - 1 ] print (s6) #DCBA #倒置 s7 = s[:: - 1 ] print (s7) #FEDCBA |
b.大小写变换
|
s = 'alxeaAa' #首字母大写 s1 = s.capitalize() print (s1) s2 = s.upper() #全部大写 s21 = s.lower() #全部小写 print (s2,s21) s3 = s.swapcase() #反转大小写 print (s3) #每个首字母大写 s4 = 'abc asc asd' s5 = s4.title() print (s5) |
c.居中
|
#居中 s6 = s.center( 20 , '_' ) #前后用_填充 print (s6) |
d.查找
|
s7 = s.startswith( 'a' , 0 , 3 ) #判断s[0]-s[2]首字母是否为a,真返回TRUE,假返回FALSE print (s7) #find 通过元素找索引,找不打返回-1 #index 通过元素找索引,找不打报错 s8 = s.find( 'a' ) print (s8, type (s8)) |
e.删除左右空格
|
#strip 默认删除空格 rstrip lstrip s = ' alll%' s1 = s.strip( '%' ) print (s1) |
f.计数
|
s2 = s.count( 'l' ) print (s2) |
g.替换
|
replace(old,new,count) |
h.把一个字符串分割成列表
|
s = 'b a c' s3 = s.split( ) #默认为空格 print (s3) |
1.3 int
|
i = 3 #转化为二进制所占最少位数 print (i.bit_length()) |
1.4 bool
|
#int --->str i = 1 s = str (i) #str--->int s = '123' i = int (s) #int --->bool i = 3 b = bool (i) #str--->bool 非空就是TRUE #s = ''--- >False |
|
转化成 bool 值为 False 的数据类型有: '', 0 , (), {}, [], set (), None |
以上所述是小编给大家介绍的python变量与简单数据类型详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!
原文链接:https://blog.csdn.net/zeroooorez/article/details/89192278
您可能感兴趣
- python内置函数一览表(Python面向对象程序设计构造函数和析构函数用法分析)
- python彩色字符视频代码(python将视频转换为全字符视频)
- python常见知识点整理(Python基础知识点 初识Python.md)
- python获取字符串类型(python3利用ctypes传入一个字符串类型的列表方法)
- python自动解密(python实现移位加密和解密)
- python 摄像头运用(使用Python控制摄像头拍照并发邮件)
- python解析csv文件存到数据库(python读取csv和txt数据转换成向量的实例)
- pythontkinter项目界面(python Tkinter版学生管理系统)
- anaconda如何配置python3.7(WIn10+Anaconda环境下安装PyTorch避坑指南)
- pythonselenium查看当前页面元素(python+selenium 定位到元素,无法点击的解决方法)
- python爬虫模块教程(Python爬虫之UserAgent的使用实例)
- python函数大全详细(详解Python函数式编程—高阶函数)
- python选择排序最大最小同时排序(Python实现的插入排序,冒泡排序,快速排序,选择排序算法示例)
- python判断对象是否是某一类型(Python判断对象是否相等及eq函数的讲解)
- python可视化界面实现管理系统(python面向对象实现名片管理系统文件版)
- opencv 图像匹配python(OpenCV+Python识别车牌和字符分割的实现)
- 白T恤穿法(白t恤)
- 你怎么忘了是说先爱我(你怎么忘了如何爱我)
- 做技术难吗(技术难不难)
- 林心如是谁(林心如是谁演的)
- 泰国安全吗(泰国安全吗2023)
- 菲律宾安全吗(菲律宾安全吗)
热门推荐
- phplaravel快速开发平台(laravel框架实现去掉URL中index.php的方法)
- docker安装java8(docker安装java环境的实现步骤)
- html5课程入门(萌新的HTML5 入门指南)
- dockerfile映射目录(Dockerfile中ENTRYPOINT 和 CMD的区别说明)
- php解压压缩包(PHP 实现文件压缩解压操作的方法)
- 织梦cms模块使用方法(织梦CMS文章模型文章转入新的自定义模型的方法)
- html5怎么滚动屏幕(HTML5 video进入全屏和退出全屏的实现方法)
- html5 拍照上传(Html5在手机端调用相机的方法实现)
- html的css中怎么选择第三个子元素(CSS中 opacity的设置影响了index层数的改变的问题总结推荐)
- computer browser服务启动后停止(用户IIS APPPOOLDefaultAppPool登录失败的解决方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9