Python基本数据类型以及字符串

基本数据类型
    
    
        数字  int ,所有的功能,都放在int里
            a1 = 123
            a1 = 456
            
            - int
                将字符串转换为数字
                    a = "123"
                    print(type(a),a)

b = int(a)
                    print(type(b),b)
                    
                    num = "0011"
                    v = int(num, base=16)
                    print(v)
            - bit_lenght
                    # 当前数字的二进制,至少用n位表示
                    r = age.bit_length()
            
        字符串  str
            s1 = "asdf"
            s2 = "asdffas"
            
            # test = "aLex"
            # 首字母大写
            # v = test.capitalize()
            # print(v)

# 所有变小写,casefold更牛逼,很多未知的对相应变小写
            # v1 = test.casefold()
            # print(v1)
            # v2 = test.lower()
            # print(v2)

# 设置宽度,并将内容居中
            # 20 代指总长度
            # *  空白未知填充,一个字符,可有可无
            # v = test.center(20,"中")
            # print(v)

# 去字符串中寻找,寻找子序列的出现次数
            # test = "aLexalexr"
            # v = test.count(‘ex‘)
            # print(v)

# test = "aLexalexr"
            # v = test.count(‘ex‘,5,6)
            # print(v)

# 欠
            # encode
            # decode

# 以什么什么结尾
            # 以什么什么开始
            # test = "alex"
            # v = test.endswith(‘ex‘)
            # v = test.startswith(‘ex‘)
            # print(v)

# 欠
            # test = "12345678\t9"
            # v = test.expandtabs(6)
            # print(v,len(v))

# 从开始往后找,找到第一个之后,获取其未知
            # > 或 >=
            # test = "alexalex"
            # 未找到 -1
            # v = test.find(‘ex‘)
            # print(v)

# index找不到,报错   忽略
            # test = "alexalex"
            # v = test.index(‘8‘)
            # print(v)

# 格式化,将一个字符串中的占位符替换为指定的值
            # test = ‘i am {name}, age {a}‘
            # print(test)
            # v = test.format(name=‘alex‘,a=19)
            # print(v)

# test = ‘i am {0}, age {1}‘
            # print(test)
            # v = test.format(‘alex‘,19)
            # print(v)

# 格式化,传入的值 {"name": ‘alex‘, "a": 19}
            # test = ‘i am {name}, age {a}‘
            # v1 = test.format(name=‘df‘,a=10)
            # v2 = test.format_map({"name": ‘alex‘, "a": 19})

# 字符串中是否只包含 字母和数字
            # test = "123"
            # v = test.isalnum()
            # print(v)
            
        列表   list
            ...
        元祖   tuple
            ...
        字典   dict
            ...
        
        布尔值 bool
            ...

原文地址:https://www.cnblogs.com/wangyue0925/p/8927109.html

时间: 2024-08-24 00:41:05

Python基本数据类型以及字符串的相关文章

Python基础数据类型之字符串

Python基础数据类型之字符串 一.Python如何创建字符串 在python中用引号将一些文本包起来就构成了字符串(引号可以是单引号.双引号.单三引号,双三引号,它们是完全相同的) >>> str1 = 'hello' >>> str2 = "hello" >>> str3 = '''hello''' >>> str4 = """hello""" &g

python基本数据类型之字符串(三)

python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法和相应的判断方法放在一起进行讲解. 这些方法包括:capitalize.casefold.lower\islower.upper\isupper.maketrans\translate.swapcase.title\istitle 1.capitalize.title.istitle capital

python基本数据类型之字符串(四)

python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值都是bool类型(True\False). 方法包括:startswith\endswith.isalnum\isalpha.isdecimal\isdigit\isnumeric.isidentifier.isprintable.isspace 1.startswith.endswith 这两个方

python基本数据类型之字符串(五)

python基本数据类型之字符串(五) 遍历与查找 python中的字符串属于可迭代对象,通过一些方法可以遍历字符串中的每一个字符.而查找的方法主要有两个:find与index. 1.字符串的遍历 字符串的遍历可以使用for循环. s = 'goodhappynewyear' li = [] for item in s: li.append(item) print(li) 打印结果: ['g', 'o', 'o', 'd', 'h', 'a', 'p', 'p', 'y', 'n', 'e',

python基础数据类型----整数 ,字符串【常用操作方法】,布尔值,for循环

Python基础数据类型(4.29) bool str int 三者之间的转换 str索引切片,常用操作方法 for循环(大量的练习题) 1.基础数类型总览 整数(int) ,字符串(str),布尔值(bool),列表(list),元组(tuple),字典(dict),集合(set). 10203 123 3340 int 主要用于计算+- * / 等等 '今天吃了没?' str 存储少量的数据,并进行相应的操作.str1 + str2, str *int , 索引,切片, 其他操作方法 Tru

Python的数据类型--数字--字符串

先讲一个概念 在Python里,一切皆对象,对象基于类创建 所以,以下这些值都是对象: "wupeiqi".38.['北京', '上海', '深圳'],并且是根据不同的类生成的对象. 1.变量类型与对象 1.程序中需要处理的状态很多,就要有不同类型的变量值来表示, 在python中所有数据都是围绕对象这个概念来构建的,对象包含一些基本的数据类型:数字,字符串,列表,元组,字典等 程序中存储的所有数据都是对象,包含了1.身份(id),2.类型(type),3值(通过变量名来查看)每个对象

【python】数据类型,字符串和编码

python笔记,写在前面:python区分大小写1.科学计数法,把10用e代替,1.23x10·9就是 1.23e9                            或者 0.00012就是1.2e-42.转义字符 \ 或者 r''[相当于C#里的@]r代表 raw string里边的\不转义   要表示  \   就要写成 \\   换行符 可以用 '''内容''' ,也可以前面加上r显示          例子:               print('''1           

python之数据类型(字符串)

字符串用双引号" "或单引号' '都可以. (一)字符串拼接 代码: 1 s1 = "这是数字: " 2 p = 99.8 3 # 使用str()将数值转换成字符串 4 print(s1 + str(p)) 5 # 字符串直接拼接数值,程序报错 6 print(s1 + p) 结果: 这是数字: 99.8 Traceback (most recent call last): File "D:/code/python_code/python_pickle.p

python 基础数据类型之字符串02

1.字符串去除空格 # # strip(self, chars=None) #去除字符串两端空格 # lstrip(self, chars=None) #去除字符串左端空格 # rstrip(self, chars=None) #去除字符串右端空格 程序: str1 = "  hello world!  " print str1.strip() print str1.lstrip() print str1.rstrip() 运行结果: hello world! hello world!