学习Python字符串

字符串不可变序列,可以用  +  实现字符串拼接

num()函数将字符串变为数字

简述 ascii、unicode、utf-8、gbk 的关系

ascii 是最早美国用的标准信息交换码,把所有的字母的大小写,各种符号用 二进制来表示,共有256中,加入些拉丁文等字符,1bytes代表一个字符,

Unicode是为了统一世界各国语言的不用,统一用2个bytes代表一个字符,可以表达2**16=65556个,称为万国语言,特点:速度快,但浪费空间,

可以用在内存处理中,兼容了utf-8,gbk,ASCII,

utf-8 为了改变Unicode的这种缺点,规定1个英文字符用1个字节表示,1个中文字符用3个字节表示,特点;节省空间,速度慢,用在硬盘数据传输,网络数据传输,相比硬盘和网络速度,体现不出来的,

gbk  是中文的字符编码,用2个字节代表一个字符,

不同字符占的字节不同,要计算zifu字符串长度,要了解字符所占的字节数,

在Python中数字,英文,小数点,下划线,空格占一个字节;汉字可能占用2~4个字节,占几个字节取决于字符编码:

汉字在GBK 或者GB2312 时占2字节,utf8unicode一般3或4字节

len()计算字符串长度

不区分英文,数字,汉字。都作为一个字符计算。

str1 = "人生苦短,我用Python!"
length = len(str1)
print(length)
print(str1.encode())    #utf 8
print(str1.encode('gbk'))

14
b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python\xef\xbc\x81'
b'\xc8\xcb\xc9\xfa\xbf\xe0\xb6\xcc\xa3\xac\xce\xd2\xd3\xc3Python\xa3\xa1'
 

字符串也支持切片:

str1 = "人生苦短,我用Python!"
#String[start:end:step] start包括 end不包括 step 步长
print(str1[1])      #下标为1
print(str1[5:])     #从第六个开始到后边
print(str1[:5])     #下标从0~4
print(str1[2:5])    #下标从2~4
print(str1)


我用Python!
人生苦短,
苦短,
人生苦短,我用Python!
字符串的分割:

str.split(sep,maxsplit)

sep 分隔符(以什么为分割)maxsplit(最大分割次数)

合并字符串:String.join(iterable)

String 合并时的分隔符

iterable 迭代对象

检索字符串:count()

检索指定字符串在另一个字符串中出现的次数,如果检索的zi

 

字符串不存在返回0,否则返回出现的次数。

find()

检索是否包含指定字符串,如检索的字符串不存在返回-1,否则返回首次出现的位置。

也可以用 in 判断指定字符串是否存在        ‘str’ in ‘str2’

index()检索是否包含指定字符串,指定字符串不存在,则会报异常(出现的位置)

startwith() 是否以指定字符串开头

endswith()结尾

str.lower() 字符串转小写

str.upper()   转大写

strip()       去掉左右两侧空格与特殊zifu字符trip()

rstrip()

字符串的格式化:

template1 = '编号: %9d\t公司名: %s \t 官网: http://www.%s.com'        #数字9占9位 默认10进制 右对齐
template2 = '编号: %09d\t公司名: %s \t 官网: http://www.%s.com'        #空格用0补位
template3 = '编号: %-09d\t公司名: %s \t 官网: http://www.%s.com'       #左对齐
template4 = '编号: %+09d\t公司名: %s \t 官网: http://www.%s.com'       #显示符号(正负)
template5 = '编号: %03d\t公司名: %s \t 官网: http://www.%s.com'        #占3位
template6 = '编号: %09.2f\t公司名: %s \t 官网: http://www.%s.com'      #浮点型 9位 小数点后保留2位以0补位
con = (7.333, '百度', 'baidu')
print(template1%con)
print(template2%con)
print(template3%con)
print(template4%con)
print(template5%con)
print(template6%con)

编号:         7    公司名: 百度      官网: http://www.baidu.com
编号: 000000007    公司名: 百度      官网: http://www.baidu.com
编号: 7            公司名: 百度      官网: http://www.baidu.com
编号: +00000007    公司名: 百度      官网: http://www.baidu.com
编号: 007    公司名: 百度      官网: http://www.baidu.com
编号: 000007.33    公司名: 百度      官网: http://www.baidu.com
 

format方式:

 

 

 

 

 

# 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)

            # 断句 (6)个一组 用空格填充
            # test = "12345678\t9"
            # v = test.expandtabs(6)
            # print(v,len(v))

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

find()    rfind()   查找子串首次,最后一次出现的位置不存在则返回-1

index()   rindex()查找子串首次,最后一次出现的位置不存在则抛出异常

            # 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)

 

*******************************************************************************

                    # 12 是否是字母,汉子
                        # test = "as2df"
                        # v = test.isalpha()
                        # print(v)

                    # 13 当前输入是否是数字
                        # test = "二" # 1,②
                        # v1 = test.isdecimal()
                        # v2 = test.isdigit()
                        # v3 = test.isnumeric()
                        # print(v1,v2,v3)

      
                    # 14 是否存在不可显示的字符
                        # \t   制表符
                        # \n   换行
                        # test = "oiuas\tdfkj"
                        # v = test.isprintable()
                        # print(v)

                    # 15 判断是否全部是空格
                        # test = ""
                        # v = test.isspace()
                        # print(v)

                    # 16 判断是否是标题
                        # test = "Return True if all cased characters in S are uppercase and there is"
                        # v1 = test.istitle()
                        # print(v1)
                        # v2 = test.title()
                        # print(v2)
                        # v3 = v2.istitle()
                        # print(v3)

                    # 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接
                        # test = "你是风儿我是沙"
                        # print(test)
                        # # t = ' '
                        # v = "_".join(test)
                        # print(v)

                    # 18 判断是否全部是大小写 和 转换为大小写
                        # test = "Alex"
                        # v1 = test.islower()
                        # v2 = test.lower()
                        # print(v1, v2)

                        # v1 = test.isupper()
                        # v2 = test.upper()
                        # print(v1,v2)
                    # 19
                        # 移除指定字符串
                        # 有限最多匹配
                        # test = "xa"
                        # # v = test.lstrip('xa')
                        # v = test.rstrip('9lexxexa')
                        # # v = test.strip('xa')
                        # print(v)

                        # test.lstrip()
                        # test.rstrip()
                        # test.strip()
                        # 去除左右空白
                        # v = test.lstrip()
                        # v = test.rstrip()
                        # v = test.strip()
                        # print(v)
                        # print(test)
                        # 去除\t \n
                        # v = test.lstrip()
                        # v = test.rstrip()
                        # v = test.strip()
                        # print(v)

                    # 20 对应关系替换
                        # test =  "aeiou"
                        # test1 = "12345"

                        # v = "asidufkasd;fiuadkf;adfkjalsdjf"
                        # m = str.maketrans("aeiou", "12345")
                        # new_v = v.translate(m)
                        # print(new_v)

                    # 21 分割为三部分
                        # test = "testasdsddfg"
                        # v = test.partition('s')
                        # print(v)
                        # v = test.rpartition('s')
                        # print(v)

                    # 22 分割为指定个数
                        # v = test.split('s',2)
                        # print(v)
                        # test.rsplit()


                    # 23 分割,只能根据,true,false:是否保留换行
                        # test = "asdfadfasdf\nasdfasdf\nadfasdf"
                        # v = test.splitlines(False)
                        # print(v)

                    #  24 以xxx开头,以xx结尾
                        # test = "backend 1.1.1.1"
                        # v = test.startswith('a')
                        # print(v)
                        # test.endswith('a)

                    # 25 大小写转换
                        # test = "aLex"
                        # v = test.swapcase()
                        # print(v)

                    # 26 字母,数字,下划线 : 标识符 def  class
                        # a = "def"
                        # v = a.isidentifier()
                        # print(v)


                    # 27 将指定字符串替换为指定字符串
                        # test = "alexalexalex"
                        # v = test.replace("ex",'bbb')
                        # print(v)
                        # v = test.replace("ex",'bbb',2)
                        # print(v)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值