首页 > 编程语言 >python学习第二天之基本数据类型及其方法

python学习第二天之基本数据类型及其方法

时间:2024-06-21 15:59:34浏览次数:14  
标签:输出 python 数据类型 test 第二天 字符串 print -- True

python的基本数据类型

1.数字 int
2.字符串 str
3.布尔型 bool
4.列表 list
5.元组 tuple
6.字典 dict

一个一个来看,分别梳理各自的方法。

一、数字 int
1.bit_length -- 得到指定数值的二进制的长度
a=10
print(a.bit_length)
输出:4

因为a=10,而10的二进制数是1010,刚好是4位,所以a.bit_length是4

2.conjugate(self, *args, **kwargs) --获取共轭复数
a = 3 + 4j
b = a.conjugate()
print(b)
输出:3-4j

3.from_bytes(cls, bytes, byteorder, *args, **kwargs) --将字节数据转化为整数,括号里带星号的可以省略。 (这个要特别注意,我没理解什么意思)
a= b'\x00\n'
b=int.from_bytes(a,byteorder='big')
print(b)
输出:10

4.to_bytes(self, length, byteorder, *args, **kwargs) --把int类型转bytes
a=10
b=a.to_bytes(2,byteorder='big')
print(b)
输出:b'\x00\n'

二、字符串

  1. capitalize()
    --把字符串中的首字母大写,其他字母全部变成小写
    a = 'woRld'
    b = a.capitalize()
    print(b)
    输出:World

  2. casefold()
    --把字符串中的所有字符变成小写
    a = 'WorLd'
    b = a.casefold()
    print(b)
    输出:world

  3. center(width, fillchar=None)
    --把字符串放在中间,总长度是width,fillchar默认为None
    a = 'world'
    b = a.center(15,'*')
    print(b)
    输出:world

  4. count(sub, start=None, end=None)
    --用于统计字符串里某个字符或子字符串出现的次数。
    sub -- 搜索的子字符串
    start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
    end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

a = 'tested'
b = a.count('e',0,5)
print(b)
输出:2

  1. encode(encoding='utf-8', errors='strict')
    --以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。
    a = 'world'
    b = a.encode('utf-8','strict')
    print(b)
    输出:b'world'

  2. endswith(suffix, start=None, end=None)
    --判断字符串是否以某个字符结尾,返回bool型
    a = 'world'
    b = a.endswith('d')
    c = a.endswith('d',1,5)
    d = a.endswith('l')
    print(b)
    print(c)
    print(d)
    输出:True
    True
    False

  3. expandtabs(tabsize=8)
    ---对字符串每8位做一次分割,之后的用空格补全。如tabsize=8,字符串是abcde\tdljaljlx\tg,则第一个\t是3个空格,第二个\t是8个空格。
    test = 'abcde\tdljaljlx\tg'
    v = test.expandtabs(8)
    print(v)
    输出:abcde dljaljlx g

  4. find(sub, start=None, end=None) ***平时用这个
    --获取字符串中某个字符所在的索引值
    a = 'world'
    b = a.find('d')
    print(b)
    输出:4

  5. index(sub, start=None, end=None) ***一般不用这个,以后也不要用它。
    --获取字符串中某个字符所在的索引值
    a = 'hello'
    b = a.index('e')
    print(b)
    输出:1

  6. format(self, *args, **kwargs)
    --格式化输出
    a = 'hello'
    b = 'world'
    print("你好的英文是{},世界的英文是{}".format(a,b))
    输出:你好的英文是hello,世界的英文是world

a = 'i am {name},age is {age}'
v = a.format(name='jack',age=10)
print(v)
输出:i am jack,age is 10

a = 'i am {0},age is {1}'
v = a.format('jack',10)
print(v)
输出:i am jack,age is 10

  1. format_map(self, mapping)
    --用10一样是格式化输出,区别就是这里用字典做对应关系。
    a = 'i am {name},age is {age}'
    v = a.format_map({'name':'jack','age':10})
    print(v)
    输出:i am jack,age is 10

  2. isalnum()
    --检测字符串是否由字母或数字组成
    a1 = 'hello'
    a2 = 'hello123'
    a3 = 'he_llo123'
    a4 = '1234'
    b = a1.isalnum()
    c = a2.isalnum()
    d = a3.isalnum()
    e = a4.isalnum()
    print(b)
    print(c)
    print(d)
    print(e)
    输出:
    True
    True
    False
    True

  3. isalpha()
    --检测字符串中是否只包含字母
    a1 = 'hello'
    a2 = 'hello123'
    a3 = 'he_llo123'
    b = a1.isalpha()
    c = a2.isalpha()
    d = a3.isalpha()
    print(b)
    print(c)
    print(d)
    输出:
    True
    False
    False

  4. isdecimal() ***写程序用的最多的是这个
    --检测字符串中是否只包含十进制数字
    str1 = '123456'
    str2 = 'abcd1234'
    str3 = '②'
    a = str1.isdecimal()
    b = str2.isdecimal()
    c = str.isdecimal()
    print(a)
    print(b)
    print(c)
    输出:
    True
    False
    False

  5. isdigit()
    --判断字符串中是否仅含有数字
    str1 = '123456'
    str2 = 'abcd1234'
    str3 = '②'
    a = str1.isdigit()
    b = str2.isdigit()
    c = str3.isdigit()
    print(a)
    print(b)
    print(c)
    输出:
    True
    False
    True

  6. isnumeric()
    --判断字符串中是否仅含有数字
    test1 = '②'
    test2 = '1234'
    test3 = 'abcd1234'
    v1 = test1.isnumeric()
    v2 = test2.isnumeric()
    v3 = test3.isnumeric()
    print(v1)
    print(v2)
    print(v3)
    输出:
    True
    True
    False

  7. isidentifier()
    --检测字符串是否为python的有效的标识符(变量)
    *标识符只能由下划线或字母开始,不能是数字
    *标识符不能含有除了下划线之外的其他特殊字符
    str1 = 'a123'
    str2 = '1a23'
    str3 = '_123'
    print(str1.isidentifier())
    print(str2.isidentifier())
    print(str3.isidentifier())
    输出:
    True
    False
    True

  8. islower()
    --检测字符串中包含的字母全部是小写字母
    str1 = 'a123'
    str2 = 'Aa23'
    str3 = 'abc'
    print(str1.islower())
    print(str2.islower())
    print(str3.islower())
    输出:
    True
    False
    True

  9. isprintable()
    --判断字符串中是否为可打印的字符
    test1 = ' '
    test2 = 'hello '
    test3 = 'hello\tworld'
    v1 = test1.isprintable()
    v2 = test2.isprintable()
    v3 = test3.isprintable()
    print(v1)
    print(v2)
    print(v3)
    输出:
    True
    True
    False

  10. isspace()
    --判断字符串是否只包含空格包括\n \t
    test1 = 'hello world'
    test2 = ' '
    test3 = '\n'
    v1 = test1.isspace()
    v2 = test2.isspace()
    v3 = test3.isspace()
    print(v1,v2,v3)
    输出:False True True

  11. istitle()
    --判断字符串是否为标题,标题的特定就是字符串中包含的所有的英文单词全部都是大写字母开头,且只能是首字母大写。
    test1 = 'HEllo World'
    test2 = 'Hello World'
    test3 = 'Hello 你好 World'
    v1 = test1.istitle()
    v2 = test2.istitle()
    v3 = test3.istitle()
    print(v1,v2,v3)
    输出:False True True

  12. isupper()
    --判断字符串中的字母是否全部都是大写
    test1 = 'hello world'
    test2 = 'HELLO WORLD'
    test3 = 'HELLO 你好 WORLD'
    v1 = test1.isupper()
    v2 = test2.isupper()
    v3 = test3.isupper()
    print(v1,v2,v3)
    输出:False True True

  13. join(iterable) ***非常重要
    --把字符串中的每一个字符用特定的符号连接起来,包括空格。
    test = 'hello世界'
    a = '_'
    v = a.join(test)
    print(v)
    输出:h_e_l_l_o_世_界

  14. ljust(width, fillchar=None)
    rjust(width, fillchar=None)
    --ljust是字符串左对齐,然后右边补齐width的长度。rjust是字符串右对齐,然后左边补齐width的长度。
    test = 'hello世界'
    v1 = test.ljust(10)
    v2 = test.rjust(10)
    print(v1)
    print(v2)
    输出:
    hello世界
    hello世界

  15. lower()
    --把字符串中所有的英文字符都变成小写
    test = 'HELLo'
    v = test.lower()
    print(v)
    输出:hello

  16. lstrip(chars=None) --删除字符串左边的字符
    rstrip(chars=None) --删除字符串右边的字符
    strip(chars=None) --删除字符串两边的字符

--举例如下:
test1 = ' hello '
test2 = 'hello'
v1 = test1.lstrip()
v2 = test2.lstrip('h')
v3 = test1.rstrip()
v4 = test2.rstrip('o')
v5 = test1.strip()
print(v1)
print(v2)
print(v3)
print(v4)
print(v5)
输出:
hello
ello
hello
hell
hello

  1. maketrans(*args, **kwargs)
    --用于字符串转换然后生成新的字符串
    test1 = 'abcdefg'
    v = test1.maketrans('cd','mn')
    result = test1.translate(v)
    print(result)
    输出:abmnefg

  2. partition(sep)
    --根据指定的分隔符从左边开始查找将字符串进行分割
    test = 'abcdcefg'
    v = test.partition('c')
    print(v)
    输出:
    ('ab', 'c', 'dcefg')

  3. rpartition(sep)
    --根据指定的分隔符从右边边开始查找将字符串进行分割
    test = 'abcdcefg'
    v = test.rpartition('c')
    print(v)
    输出:
    ('abcd', 'c', 'efg')

  4. replace(old, new, count=None)
    --替换字符串中的字符
    test = 'abcdcefg'
    v1 = test.replace('c','m')
    v2 = test.replace('c','m',1)
    v3 = test.replace('c','m',3)
    print(v1)
    print(v2)
    print(v3)
    输出:
    abmdmefg
    abmdcefg
    abmdmefg

  5. rfind(sub, start=None, end=None)
    ---从右边开始数,找到字符串中某个字符的索引值
    test = 'abcdecfg'
    v = test.rfind('c')
    print(v)
    输出:5

  6. split(sep=None, maxsplit=-1)
    --分割字符串,以某个字符进行分割,但不包含该字符
    test = 'abcdecfg'
    v1 = test.split()
    v2 = test.split('c')
    print(v1)
    print(v2)
    输出:
    ['abcdecfg']
    ['ab', 'de', 'fg']

  7. rsplit(sep=None, maxsplit=-1)
    --同split一样,只不过split如果加了maxsplit参数,表示从左边开始查找,限定可分割次数。rsplit则表示从右边开始查找
    test = 'abcdecfcgdagag'
    v = test.rsplit('c',2)
    print(v)
    输出:['abcde', 'f', 'gdagag']

  8. splitlines(keepends=None)
    --按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符,默认是False.
    test = 'abcdecfc\ngdagag'
    v1 = test.splitlines()
    v2 = test.splitlines(True)
    print(v1)
    print(v2)
    输出:
    ['abcdecfc', 'gdagag']
    ['abcdecfc\n', 'gdagag']

  9. startswith(prefix, start=None, end=None)
    --判断字符串是否已某个字符开头
    test = 'abcdecfcgdagag'
    v1 = test.startswith('d',3,-1)
    v2 = test.startswith('a')
    v3 = test.startswith('c')
    print(v1)
    print(v2)
    print(v3)
    输出:
    True
    True
    False

  10. swapcase()
    --对字符串中大小写字母进行转换
    test = 'AbCd'
    v = test.swapcase()
    print(v)
    输出:aBcD

  11. title()
    --把字符串转换为标题,其实就是字符串中的每个单词首字母大写
    test = 'hello world'
    v = test.title()
    print(v)
    输出:Hello World

  12. translate(table)
    ?????

  13. upper()
    --字符串中所有字母大写
    test = 'hello world'
    v = test.upper()
    print(v)
    输出:HELLO WORLD

  14. zfill(width)
    --填充字符串,只能以0填充,没什么实际用途。
    test = 'hello'
    v = test.zfill(10)
    print(v)
    输出:00000hello

标签:输出,python,数据类型,test,第二天,字符串,print,--,True
From: https://www.cnblogs.com/kkbest/p/18255632

相关文章

  • JavaScript的学习之旅之基本数据类型
    一、字面量(常量)和变量字面量:不可变的数据,一般位于等式的右边变量:可变的数据,位于等式的左边<script> //声明一个变量 //用关键字var声明 vara; //要为变量赋值 a=456 a=678 //声明和赋值可以同时进行 varb=34; console.log(b);</script>二、标识符......
  • python 趣味习题_递归函数(炸弹迷宫的走法)
    @[toc]python学习中,常会遇到一些百思不得其解的难题,但有时“灵光一现”找准方法,难题便会迎刃而解。本专栏旨在记录本人解决问题的思考方法,及实现过程。有更好方法或对程序执行有疑问的伙伴,可在评论区留言,共同讨论。题目要求题目描述:在一串连续的迷宫(房间编号为1-11的......
  • vscode python编程入门与插件推荐
    使用背景作者是一名ai测开工程师,工作环境中通常会使用到pythonshellpostgresql数据库jupyter,shh工具git版本控制等,因此向选用一个适合自己的文本编辑软件,经过长时间磨合,最终选择VScode作为我的工作软件优点内存占用率低,集成度高,开源免费插件推荐AI助手:codegeex此插件......
  • 3-数据类型
    强类型语言:所有变量必须先定义后使用Java的数据类型分为两大类基本类型引用类型基本数据类型整数浮点数字符布尔值//整数intnum1=10;//最常用bytenum2=20;shortnum3=30;longnum4=30L;//long类型要做数字后面加个L......
  • Red Hat系列的Linux发行版中如何安装python3.9
    该发行版使用yum或dnf作为包管理器:首先更新你的系统和已有的包。在终端输入以下命令:sudoyum-yupdate 安装依赖项:sudoyum-ygroupinstall"DevelopmentTools"sudoyum-yinstallopenssl-develbzip2-devellibffi-devel 下载Python3.9的源代码:wget......
  • 一个简单的python脚本,把latex项目的调用资源放在同一级,以便arxiv
    据说上传arxiv时所有资源需要在同一目录,也就是不能有文件夹(只是据说,有人说有文件夹也行,我没试过),所以写了一个简单的小脚本把latex项目的资源(主要是图片)放在和.tex一个路径下:importosimportshutiltex_file='main.tex'img_folder='imgs'encoding_type='utf-8'withopen......
  • 第二章:变量、数据类型、运算符、表 达式
    一、变量1.概念:计算机中的一块内存空间,存储数据的基本单元2.变量的组成部分:数据类型、变量名、数据3.语法:(1)先声明,再赋值:数据类型变量名;//声明变量名=值; //赋值(2)声明的同时并赋值:数据类型变量名=值;(3)同时定义多个相同类型的变量:数据类型......
  • Python梯度提升决策树的方法示例
    梯度提升决策树(GradientBoostingDecisionTree,简称GBDT)是一种基于集成学习的算法,它通过构建多个决策树模型,并将它们组合在一起来实现更好的预测性能。GBDT的核心思想是在每轮迭代中,根据当前模型的残差(真实值与预测值之差)来训练一个新的决策树,然后将这个新树添加到模型中,以不断减......
  • 用Nuitka打包 Python,效果竟如此惊人!
    目录为什么选择Nuitka?Nuitka的工作原理Nuitka的工作流程大致如下:安装Nuitka实战案例示例代码打包程序运行可执行文件进阶技巧优化选项多文件项目打包第三方库使用Python开发一个程序后,将Python脚本打包成独立可执行文件是一项常见需求。Nuitka是一个Python......
  • 用Python执行JavaScript代码,这些方法你不可不知!
    目录1、PyExecJS:轻量级桥梁......