python之字符串类型介绍 & 内置函数使用

您所在的位置:网站首页 python字符处理函数有哪些 python之字符串类型介绍 & 内置函数使用

python之字符串类型介绍 & 内置函数使用

2024-07-02 19:58| 来源: 网络整理| 查看: 265

python之字符串类型(str)

在python中字符串常用的表示方法有3种 str类型是不可变的,tuple也是不可变的

name = 'hello world' name = "hello world" name = '''hello world''' 1 str类型的操作符

在字符串中有多种运算符供使用,常用的如下.

== :比较值是否相等 is :比较变量指向的内存地址是够一致 in :一个字符串是否属于另一个字符串的子集 not in r :保证按原来格式进行输出 %s、d、f:分别代表占位符(字符型、整型、浮点型) []: 取出对应坐标或者坐标范围内的字符串子串

# h e l l o w o r l d # 0 1 2 3 4 5 6 7 8 9 # -10-9 -8 -7 -6 -5 -4 -3 -2 -1 testname = 'helloWorld' print(testname[0]) # h print(testname[9]) # d print(testname[0:]) # helloWorld print(testname[:9]) # helloWorld print(testname[0:10]) # helloWorld [0,10) print(testname[-10:-1]) # helloWorl [-10,-1) print(testname[0:-1]) # helloWorl print(testname[:]) # helloWorld print(testname[::]) # helloWorld print(testname[::-1]) # dlroWolleh print(testname[::-2]) # drWle 其中:-2代表步长为2的倒序取值 print(testname[-1:-10:-1]) # dlroWolle 表示从-1 > -10的逆向顺序取值 print(testname[9:0:-1]) # dlroWolle 2 str 类型的内置函数

lower()\upper()\capitalize()\title()

2.1 大小写函数 # 常用的str内置函数有:capitalzie\lower\upper\title\is[lower\titile\upper\capitalize] #''' # capitalzie 将字符串首字母大写 # title 将字符串的每个单词的首字母大写 # lower 将字符串小写 # upper 将字符串大写 #''' s = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm9876543210' print(len(s)) # 62 code = '' import random # 循环四次,得到验证码 for i in range(4): index = random.randint(1, len(s) - 1) result = s[index] code += s[index] print("验证码", code) inputCode = input('请输入验证码:') if inputCode.lower() == code.lower(): print("输入验证码正确,成功登陆。") else: print('验证码错误,请检查后重新输入!~') 2.2 查找函数

find\rfind\lfind\index\lindex\rindex\replace

# find lfind rfind index lindex rindex replace s = 'spark flink azkaban oozie' # find(),从一个字符串中查找子字符串的第一个坐标,如果找到,返回对应坐标,如果没找到返回-1 # index(),与find类似从一个字符串中查找子字符串的坐标,如果找到,返回对应坐标,如果没找到抛出异常:ValueError: substring not found # rfind(),从字符串的右边开始查找 # replace() 将某个子字符串替换成新的子字符串 print(s.find('s')) # 0 print(s.find('s', 1)) # -1 print(s.find('f', 1, 20)) # 6,查找1-19子字符串中的 # print(s.index('x')) # ValueError: substring not found print(s.index('s')) # 0 print(s.rindex('o', 0, 21)) # 21 # 因为string类型是一种不可变的序列seq,只能通过replace()函数来替换并生成新的字符串 # ''' # spark#flink#azkaban#oozie # spark flink azkaban oozie # ''' replace = s.replace(" ", "#") print(replace, s, sep='\n') print(s.find('fl')) # 6 2.3 编码解码&起始-结束

str.encode(): 编码,默认utf-8,还可以选择gbk(中文) ,gb2313(简体中文) code.decode(): 解码,编码格式必须与解码格式保持一致

# encode()\decode() msg = '好好学习,天天向上' # 中文 # 编码 # gbk 中文 gb2313 简体中文 unicode code = msg.encode('gbk') print(code) # b'\xc9\xcf\xbf\xce\xc0\xb2\xa3\xac\xc8\xcf\xd5\xe6\xcc\xfd\xbd\xb2' # 解码 print(code.decode('gbk')) # 好好学习,天天向上 2.3 起始&结束

startwith()

endwith()

以上2个函数都是str内置函数,都是返回bool值。

# startWith()\endWith() # ''' # True # True # False # ''' print(msg.startswith('好好')) print(msg.endswith('上')) print(msg.endswith('gh')) 2.4 判断是否全为数字、字母

isdigit() :是否字符串中全为数字 ‘1234’

isalpha() : 是否全为字母 ‘abcd’

isnumeric() :是否字符串中全为数字

isspace() : 是否字符串中全为空格

# isalpha()判断是否为全字母 isdigit() 判断是否为全数字 isnumeric()是否全部为数字 isspace()是否为空字符串 s = '1234' print(s.isalpha()) # print(s.isdigit()) if s.isdigit(): print(int(s)) sum = 0 i = 1 while i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3