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 ...
随机推荐
- python中requests
#发送无参数的get请求import requests def get_html(url): res = requests.get(url) return res.text #发送无参数的post请求 ...
- plist
<a title="iphone" href="itms-services://?action=download-manifest&url=https:// ...
- PHP 函数(2)
自定义函数: $name = "fakeface"; function dispalyName(){ echo "fakeface"; } function r ...
- mybatis进阶
1.mybatis一对一映射 Student--Card <?xml version="1.0" encoding="utf-8" ?> <! ...
- sql数据库 管理处理问题--维护计划
问题:SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 MYPC/Administrator' 的信息,错误代码 0x534. [SQLSTATE 42000] ( ...
- 排序小结(C++版)
一.快速排序 #include <iostream> using namespace std; int adjust(int a[],int start,int end) { int i, ...
- Apache代理Tomcat实现session共享构建网上商城系统
一.环境介绍 二.安装配置后端服务器 三.安装配置前端服务器 四.配置Tomcat服务器实现session共享 五.构建网上商城系统 一.环境介绍 系统版本:CentOS 6.4_x86_64 Mys ...
- Hbase中的BloomFilter(布隆过滤器)
(1) Bloomfilter在hbase中的作用 Hbase利用bloomfilter来提高随机读(get)的性能,对于顺序读(scan)而言,设置Bloomfilter是没有作用的(0.9 ...
- 数据交互 ajax 初始化省
1 //初始化省 2 function initProvince() { 3 if( areaLvel == 0 ) { 4 return; 5 } 6 // 清空option 7 $("# ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...