数字类型(int):

在python 2中,数字类型可以分为整形,长整形,浮点型,以及复数。在python3中都是整形和长整形都称之为整形,且python3中没有限制。

1.int方法使用,用于转换字符类型为数字类型(注意被转换的数据类型必须是数字)
a = ''      #这里a是一个字符串,验证如下,可以用type(a)可显示出来
print(type(a))
print(a)
u = int(a) #这里u是一个数字类型的,这里的话是讲字符串类型转换为数字类型
print(type(u))
print(u)
2.bit_length(self)方法使用,这里我理解的是看这些数字或者字符串要至少占用多少的位数
num = 12
print(num.bit_length())
执行结果:
4

字符串(str):

1.capitalize  方法使用,作用是将首字母变大写:

Name = "yangxiufeng "
S = Name.capitalize()
print(S)
执行如下:
Yangxiufeng

2.casefold 方法使用,作用是将字符串里的字母变成小写:

Name = "YANGXIUFENG"
S = Name.casefold()
print(S)
执行如下:
yangxiufeng

3.center 方法使用:(内容居中,width:总长度;fillchar:空白处填充内容,默认无)还有ljust  ,rjust   指定左右边填充字符

Name = "YANGXIUFENG"
S = Name.center(20,"=") #可以指定两边的字符长度,切字符只能是一位
S1 = Name.center(20) #默认是将字符串放到指定长度的中心
print(S)
print(S1)
执行如下:
====YANGXIUFENG=====
YANGXIUFENG

4.count 方法的使用:(子序列的个数)

name = "xiaooomimming"
S = name.count('o') #count参数可以查看某个字符或者字符组合在字符串里出现的次数
S1 = name.count("o",4) #也可以用start 和end 限定未知进行范围查看出现的次数
print(S)
print(S1)
执行如下:
3
2

5.encode 方法的使用:

待续

6.endswith 方法的使用:

name = "xiaooomimming"
S = name.endswith('g') #用来确认字符串最后结尾是不是自己执行的,如果是的话将会提示True,否则是False
S1 = name.endswith('g',1,4) #用来指定范围,看范围内的字符是不是以自己指定的字符结尾,布尔值表示是与否
print(S)
print(S1)
执行如下:
True
False

7.expandtabs 方法的使用: 将字符串中的table替换成指定的空格数,从前往后数限定的数字为一组遇到"\t" 如果不足会以空格补全

name = "xiaooo\tmimming"
S=name.expandtabs(tabsize=8)
print(S)
print(name)
执行如下:
xiaooo mimming
xiaooo mimming
实例:
A = "name\tmail\tphonenameber\nyangxiufeng\t87987889@qq.com\t123456\nyangxiufeng\t87987889@qq.com\t123456\n"
print(A.expandtabs(30))
结果如下:
name                          mail                          phonenameber
yangxiufeng 87987889@qq.com 123456
yangxiufeng 87987889@qq.com 123456

7.find 方法的使用:(寻找子序列的位置,没找到的话返回 -1)

name = "xiaooomimming"
S = name.find('m') #查找字符串里是否有自己想要的字符如果有会显示字符的下标,没有的话会显示 -1表示,也可以指定范围
S1 = name.find('m',11,20)
print(S)
print(S1)
执行如下:
6
-1

8.format 方法的使用:字符串格式化,动态参数

A = '{name} is {age} years old'
S= A.format(name = 'yangxiufeng',age= 18) #指定格式书写方式用{}表示,也可以用下列的方法表示,默认传参
B = '{} is {} years old'
S1 = B.format('XIUFENG',45) #这里我们是按位置对应起来,一个字符一个坑,跟填空题差不多,只不过是按顺序而已了
print(S)
print(S1) 执行如下:
yangxiufeng is 18 years old
XIUFENG is 45 years old

9.format_map 方法的使用:

A = '{name} is {age} years old'
S= A.format_map({'name':'yangxiufeng','age':''}) #此方法和format差不多,书写格式是这样的
print(S) 执行如下:
yangxiufeng is 18 years old

10.index 方法的使用:子序列位置,如果没找到,报错

A = 'yangxiufeng'
B = A.index('u') #查找指定字符的下标
C = A.index('t') #查找不存在字符串里的字符时会报错,这个的话是没有find方法好用,报错会给(-1)
print(B)
print(C) 执行如下:
6
Traceback (most recent call last):
File "E:/new/文件io/Nine.py", line 5, in <module>
C = A.index('t') #查找不存在字符串里的字符时会报错,这个的话是没有find方法好用,报错会给(-1)
ValueError: substring not found

11. isalnum 方法的使用:是否是字母和数字,如果有其他的符号会出现False

A = 'yangxiufeng-*'
B = 'yangxiufeng23432'
print(A.isalnum())
print(B.isalnum()) 执行如下:
False
True

12.isalpha 方法是否是字母  也是布尔值提示

A = 'yangxiufeng'
B = 'yangxiufeng1'
print(A.isalpha())
print(B.isalpha()) 执行如下:
True
False

13. isdecimal ,isdigit,isnumeric 方法:是否为数字(3个方法都可以用来判断是否为普通的十进制数字,isdigit 方法可以判断特殊的数字以及十进制数字,isnumeric功能强大也可以额外判断中文的数字)

test = "223"        #纯数字判断
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3) 执行结果:
True True True
test = "223②"        #特殊数字判断
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3) 执行结果:
False True True
test = "223②二"        #加中文字符判断
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3) 执行结果:
False False True

												

python 数字以及字符串(方法总结,有的可能理解错误)的更多相关文章

  1. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  2. Python数字、字符串

    1. 数字 byte 在python3中最重要的特性是对文本和二进制数据做了更加清晰的区分,python3不会以任意隐式方式混用字节型和字符型,也因此在python3中不能拼接字符串和字节包(pyth ...

  3. Python数字,字符串

    数字 支持整数,浮点数,和奇怪的类型,如复数. 特殊的运算符为**,表示次方操作,如2**100,表示2的100次方. len()可以得到一个字符串对象的长度,str()可以将数字转换为字符串. pr ...

  4. python 常用的字符串方法

    st = ' hello Kitty 'str = 'hello {name} {age}' #print(st.format(name='fadfa'))#常用的字符串方法print(st.coun ...

  5. 【python基础】字符串方法汇总

    一.声明 0-多个字符组成的有序序列; 二.特点 1. 字符串是一个不可变的数据类型 2.字符串是有序的 三.索引 下标:'abcde' 1.从左到右, 0, 1,2, ... 2.从右到左, 索引值 ...

  6. python 基础之字符串方法

    字符串 print('chenxi'*8) 测试 D:\python\python.exe D:/untitled/dir/for.py chenxichenxichenxichenxichenxic ...

  7. python 数字和字符串转换问题

    一.python中字符串转换成数字 (1)import string tt='555' ts=string.atoi(tt) ts即为tt转换成的数字 转换为浮点数 string.atof(tt) ( ...

  8. 【python 3】 字符串方法操作汇总

    基础数据类型:str 1.1  字符串大小写转换 所有字母大写 : string.upper() 所有字母小写 : string. lower() 第一个单词的第一个字母大写,其他字母小写 :  st ...

  9. python基础 while 字符串方法 运算符

    一.while 1.while 死循环 f=True while f: print(1) print(2) 2.while 活循环 ①.正序 count = 1 while count <= 5 ...

随机推荐

  1. 设置ul的指定li 样式

    设置ul的最后li 的样式 .custom-consumerIndex .card-content .list-block ul li:last-child .item-inner { border- ...

  2. vue项目中,使用vue-awesome-swiper插件实现轮播图

    一.安装 npm install vue-awesome-swiper 二.项目中引入 import 'swiper/dist/css/swiper.css'import {swiper,swiper ...

  3. ;html5斜体字

    font-style:italic; italic|oblique|normal 依次倾斜,越来越邪:

  4. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.

    好久没有冒泡了,最近在新环境上搭建应用时,启动报错: INFO: Illegal access: this web application instance has been stopped alre ...

  5. centos 系统上如何把python升级为3

    第一种方式: SCL 源目前由 CentOS SIG 维护,除了重新编译构建 Red Hat 的 Software Collections 外,还额外提供一些它们自己的软件包. 该源中包含不少程序的更 ...

  6. Angular/cli 安装(windows环境)。

    1.卸载先前安装的Angular/cli npm uninstall -g angular-clinpm uninstall --save-dev angular-clinpm uninstall - ...

  7. 实现在WebView中返回上一级

    代码 import React, {Component} from 'react'; import {Platform, View, WebView, BackHandler,Dimensions,S ...

  8. 忽略SIGPIPE信号

    #include <stdlib.h> #include <sys/signal.h> void SetupSignal() { struct sigaction sa; // ...

  9. How to Animate UILabel textColor Properties

    How to Animate UILabel Properties UILabel properties cannot be easy animated due to some various rea ...

  10. 使用spark访问hive错误记录

    在spark集群中执行./spark-shell时报以下错误: 18/07/23 10:02:39 WARN DataNucleus.Connection: BoneCP specified but ...