python数据类型之一字符串(str)

mac2022-06-30  26

字符串是Python中最常用的数据类型之一,字符串的本质是值,就像数字一样

创建字符串的方式也很简单,只需为变量分配一个值即可

值的注意的是字符串是不可变量,不能被修改

在python3中所有的字符串均为Unicode字符串

栗子:

var = 'Hello World!'

字符串格式化

字符串格式化使用字符串的格式化操作符即%来实现

格式化字符串的%s、%d、%.nf部分称之为转换说明符,标记了需要插入的转换值的位置

%s:格式化的部分为字符串str,如果不是字符串可以强制类型转换为字符串

%d:格式化的部分为整数

%f:格式化的为浮点数,同时提供所需要保留的精度,一个句点加上需要保留的小数位数 

#%s占位符,%d整数占位符,%.nf浮点数,保留几位小数 name = input("请输入你的姓名:") age = int(input("你的年龄是:")) money = float(input("你有多少零花钱捏?")) print("姓名是%s,年龄是%d,零花钱是%.2f"%(name,age,money)) 运行结果: D:\Python\Miniconda3\python.exe D:/Python/Python3.6.0/BestTest/BestTest/Recover/Day1.py 请输入你的姓名:xxxx 你的年龄是:18 你有多少零花钱捏?666.666 姓名是xxxx,年龄是18,零花钱是666.67 Process finished with exit code 0

字符串方法

字符串中还包含多种内置的方法,这是因为字符串从string模块中继承了很多方法

find()

find方法可以用来查找一个较长字符串中的子串

str1 = 'to do or not to do ,this is a question' #find方法用来查找字符串中的子串,并返回子串在最左端(第一次出现的位置)的下标 print(str1.find(‘do’)) #如果查找的元素不存在,返回-1 print(str1.find(‘xx’)) 运行结果: D:\Python\Miniconda3\python.exe 3 -1 Process finished with exit code 0 #find还可以接收起始位置和结束位置参数,通过规定起始下标和结束下标,返回一定范围内查找的子串(顾头不顾尾) str1 = 'to do or not to do ,this is a question' print(str1.find('do',5,20)) 运行结果: D:\Python\Miniconda3\python.exe 16 Process finished with exit code 0

join()                

join方法时非常重要的字符串方法,用来连接list中的字符串元素,列表中的元素必须是字符串类型

join为split的逆方法

只能换种插入代码的方式了,server502了,呜呜呜呜。。。。

#join为字符串的方法,连接字符串列表,为split的逆方法 #如果列表中的元素不是字符串则会报错 dir1 = ['C:','adminstir','desktop'] sep ='\\' url = sep.join(dir1) print(url) 运行结果: D:\Python\Miniconda3\python.exe y C:\adminstor\desktop Process finished with exit code 0 ************************ #list中的内容不是字符串 list1 = [5,6] sep ='+' print(sep.join(list1)) 运行结果: D:\Python\Miniconda3\python.exe Traceback (most recent call last): File "D:/Python/Python3.6.0/xxxxx/xxxxx/Recover/xxx.py", line 178, in <module> print(sep.join(list1)) TypeError: sequence item 0: expected str instance, int found Process finished with exit code 1

split()

split方法是join的逆方法,将字符串按某个字符分割,返回为list的方式

#split为join的逆方法,将字符串分割为list #'\'注意转义 url = 'C:\\adminstor\desktop' dir1 = url.split('\\') print(dir1) 运行结果: D:\Python\Miniconda3\python.exe ['C:', 'adminstor', 'desktop'] Process finished with exit code 0 **************************** #当split中的内容不填写时,为按照空格分割 str1= 'no zuo no die' print(str1.split()) 运行结果: D:\Python\Miniconda3\python.exe ['no', 'zuo', 'no', 'die'] Process finished with exit code 0

strip()

strip方法返回去除两侧(不包含中间)的空格或其他字符

#strip去除字符串两侧(不包含中间)的空格或指定字符串 #strip()中的只能为空或者为字符串,默认为空 #去除字符串首尾的空格 str1 = ' username ' print(str1.strip()) 运行结果: D:\Python\Miniconda3\python.exe username Process finished with exit code 0 *************************************** #去除字符串首尾的指定字符 str1 = 'hahaha username hahaha' print(str1.strip('hahaha')) 运行结果: D:\Python\Miniconda3\python.exe username Process finished with exit code 0

 lower()

返回字符串的小写字母

#lower返回字符串的小写字母 #upper返回字符串的大写字母 #小写 str1 = 'Hello World!' print(str1.lower()) 运行结果: D:\Python\Miniconda3\python.exe hello world! Process finished with exit code 0 #大写 str1 = 'Hello World!' print(str1.upper()) 运行结果: D:\Python\Miniconda3\python.exe HELLO WORLD! Process finished with exit code 0 当在编写不区分大小写的代码时,就可以用上lower或者upper方法:例如输入用户名和密码的时候忽略大小写,只需要将存储和填写登录时的内容全部转换为大写或者小写即可 lower、upper方法还可以和strip方法一起连用,对比输入和存储的值 #upper()和strip()连用 names = ['DENNY', 'JENNY','LIMING'] Name = input('请输入用户名:') Name = (Name.strip()).upper() if Name in names: print('用户名已存在!') elif Name =='': print('用户名不能为空!') else: print('请先注册!')

  

replace() replace(‘oldstr’,‘newstr’)方法返回,某字符串的所有匹配项均被替换后)得到的字符串,类似于word文档中的“查找并替换”功能 str1 = 'no zuo no die' print(str1.replace('no','不')) 运行结果: D:\Python\Miniconda3\python.exe 不 zuo 不 die Process finished with exit code 0

  

 

转载于:https://www.cnblogs.com/Amei1992/p/8419299.html

最新回复(0)