python学习-day03:整形、字符串常用方法:
一、数字,int
1.1;
a、int(object)转化数字类型;
a=''
b=int(a)
b=b+1000
print(b)
223 <class 'int'>
答案
b、转化二进制;
v=int(num,base=2) #number以二进制的形式转化为 10进制
num='0011'
v=int(num,base=2)
print(v)
num=''
v=int(num,base=16)
print(v)
num='b'
v=int(num,base=16)
print(v)==10
3、17
答案
1.2 bit_length 查看二进制的最小表示位数;*.bit.length()
a=123
b=a.bit_length()
print(b)
结果:7
答案
二、字符串转化方法:
text="alex"
2.1、capitalize 首字母转化为大写:
text="alex"
v=text.capitalize()
print V
>>>Alex
2.2、casefold()和lower()所有字母变小写。casefold更加牛逼,很多未知的都可以对应变小写;很多国家的对应字符都可以转化:
test='alex'
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
>>
alex
alex
2.3、center(self, width, fillchar=None):width:总长度:fillchar:空白位置填充(只填一个字符 )
test='alex‘
v = test.center(20) 一共20个字符
print(v)
alex test='alex‘
v = test.center(20,’中‘)
print(v) >>>
中中中中中中中中alex中中中中中中中中
2.4、count(self, sub, start=None, end=None):计算指定字符或子序列出现的个数。可以加入:起始和结束位置。
test = "aLexalexr"
v = test.count('ex',1,5)
print(v) >>>1
2.5、encode(self, encoding='utf-8', errors='strict')和decode 后面再说!
2.6、endswith(self, suffix, start=None, end=None) 以什么(字符或者子序列)结尾。计算开始和结束的位置。这样的字符会不一样。
test = "alex"
v = test.endswith('ex')
print(v)
v = test.startswith('ex')
print(v) >>>
True
False
2.7、expandtabs(self, tabsize=8) 断句:制表符
test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
v = test.expandtabs(20)
print(v)
>>>>
username email password
laiying ying@q.com 123
laiying ying@q.com 123
laiying ying@q.com 123
2.8、查找指定序列的位置;find、index
1、find(self, sub, start=None, end=None)。从开始往后找,获取第一个的位置。后面就不继续找了。可以通过设置起始和结束位置来控制。【start,end)
2、index(self, sub, start=None, end=None)。找不到则会报错
test = "alexalex"
# 未找到 -1
v = test.find('ex')
print(v) >>>2
2.9、format(self, *args, **kwargs) 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v)
>>>>
i am {name}, age {a}
i am alex, age 19
test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v)
>>>
i am {0}, age {1}
i am alex, age 19
2.10、isalnum(self) 判断字符串中,是否只包含:字母和数字:
test = "123ww"
v = test.isalnum()
print(v) >>>
True
2.11 isalpha() 判断字符串是否全为字母;
test = "as2df"
v = test.isalpha()
print(v) >>>>
False
2.12 isnumeric()、判断十进制数 isdigit()、可以判断其他格式的数字 isdecimal()可以判断各种范围内的数字
test = "1"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
>>>>>
True True True test = "②"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
>>>
False True True test = "二"
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
>>>>
False False True
2.13、isdecimal 判断字母是合法标识符号,包括已有标量,重新命名
test = "def"
v = test.isidentifier()
print(v)
>>>
True
2.14 islower()和lower()、isupper()和upper()判断是否全部为大小写和转化大小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) v1 = test.isupper()
v2 = test.upper()
print(v1,v2) >>>>
False alex
False ALEX
2.15、isprintable()否存在不可显示的字符。
#不可显示字符
# \t 制表符
# \n 换行
test = "oiuasdfkj\n"
v = test.isprintable()
print(v) >>>
False
python学习-day03:整形、字符串常用方法:的更多相关文章
- Python学习-第二天-字符串和常用数据结构
Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...
- Python学习 day03
一.基本数据类型 python中的基本数据类型有以下几种: int -- 整数 python3中默认整数都是int型,python2中int的范围为-231~232-1(32位系统中)/ ...
- 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)
""" 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...
- Python学习杂记_3_字符串操作的常用方法
字符串操作 字符串是可以通过下标来进行取值的,但是由于字符串是不可变变量,不能通过下标来修改它的值(形式如 字符串[下标]),下标从0开始,最大下标值是字符串长度减1,即len(string)-1 P ...
- Python学习 day03打卡
今天学习的主要内容: ppython的基本数据类型: 1. python基本数据类型回顾 2.int---数字类型 4.str---字符串类型 一.python基本数据类型 1. int==>整 ...
- Python学习2——使用字符串(完整版)
""" 在C语言入门的时候字符串没有好好学习,导致后期语言根本没有入门, 更导致之后大量的codeing时间浪费,效率低下. 因此,借助这次Python入门,好好地将字符 ...
- python学习笔记(一)---字符串与列表
字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...
- python学习笔记(二)-字符串方法
python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
随机推荐
- 【接口测试】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台
参考文档: http://www.cnblogs.com/liuqi/p/5224579.html
- guava学习--hashing
128位的MurmurHash(烽火使用过): 看一下Java标准库中的非加密哈希算法你会发现少了MurmurHash,这是一个简单高效且还是分布式的算法,在许多语言中都有着很好的支持.我们并不是说要 ...
- JQ判断屏幕宽度
<script> if (screen.width < 768){....} </script>
- Ubuntu 下载 & 编译 Android5.1 源码
ustc & tsinghua android srchttps://lug.ustc.edu.cn/wiki/mirrors/help/aosphttps://mirrors.tuna.ts ...
- ajax 创建对象验证
function getValue(){ var str = $("#selectid option:selected").val(); //获得选中的值 var num = $( ...
- android-Okhttp初步使用
自从谷歌把android的请求框架换成Okhttp后,android开发人员对其的讨论就变的越来越火热,所有咱作为一枚吊丝android程序员,也不能太落后,所以拿来自己研究一下,虽然目前项目开发用的 ...
- python3使用requests登录人人影视网站
python3使用requests登录人人影视网站 继续练习使用requests登录网站,人人影视有一项功能是签到功能,需要每天登录签到才能升级. 下面的代码python代码实现了使用requests ...
- php-eclipse乱码处理
方法一:1)设置"eclipse目录下eclipse.ini文件"2)在文件结尾添加"-Dfile.encoding=UTF-8".3)重新启动eclipse, ...
- POJ-1182 分组并查集
今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...
- 北京网赛I题 hiho1391 (树状数组、区间覆盖最大值问题)
题目链接:http://hihocoder.com/problemset/problem/1391 题意:A国和B国向对方分别投射N枚和M枚导弹(发射时间,飞行时间,伤害值),同时两国各自都有防御系统 ...