python字符串操作
-
字符串类型转换及测试长度
# 只有是数字类型的字符串才能转换成整形 my_str = "100" my_num = 100 print(len(my_str))#测试字符串长度 print(str(my_num))#把int类型强制转换成str类型 print(int(my_str))#把str类型强制转换成int类型 print(type(my_num))#测试类型 print(type(my_str))#测试类型 # 运行结果 3 100 100 <class 'int'> <class 'str'>
-
输入、输出字符串
# input输入的都是字符串 my_str = input("请输入一个字符串:") print("您输入的是:%s"%my_str) # 运行结果 请输入一个字符串:5 您输入的是:5
-
组成字符串的两种方式
a = 'lao' b = 'wang' c = 'zhao' print(a+b) print("===%s==="%(a+b)) # 运行结果 laowang ===laowang===
-
字符串中的下标
name = "abcdef" print(name[0]) print(name[1]) print(name[2]) print(name[3]) print(name[4]) print(name[5]) print("********************") print(name[-1]) print(name[-2]) print(name[-3]) print(name[-4]) print(name[-5]) print(name[-6]) # 运行结果 a b c d e f ******************** f e d c b a
-
切片、字符串逆序
name = 'abcdefABCDEF' print(name[2:5]) # 切片包头不包尾 print(name[2:6]) print(name[2:-2]) # -2是最后第二位 print(name[2:-1]) # -1是最后一位 print(name[2:]) # 空是直接取到尾 print(name[2:-1:2]) # 从下标为2开始取到最后一位 步长为2 #逆序->倒序 print("********************") print("逆序->倒序") print(name[::-1]) #步长的正就是从左往右 步长为负从右往左 print(name[-1::-1]) # 运行结果 cde cdef cdefABCD cdefABCDE cdefABCDEF ceACE ******************** 逆序->倒序 FEDCBAfedcba FEDCBAfedcba
-
字符串查找-find-rfind-index-rindex
my_str = 'hello world itcast and itcastxxxcpp' # find查找字符串 从左往右 反回字符串首字母的下标 # rfind 从右往左 查找字符串 反回字符串首字母的下标 # 没有找到 反回 -1 print(my_str.find("world")) print(my_str.rfind("itcast")) print("****************************") # index跟find功能一样 只不过 没找到时反回异常 # rindex跟rfind功能一样 没找到时反回异常 print(my_str.index("world")) print(my_str.rindex("itcast")) # 运行结果 6 23 **************************** 6 23
-
字符串替换-replace
my_str = 'hello world itcast world and itcastxxxcpp' # 把小写字母world替换成大写WORLD # 从左往右依次替换 print(my_str.replace("world","WORLD")) # 替换一次 print(my_str.replace("itcast","xxx",1)) # 运行结果 hello WORLD itcast WORLD and itcastxxxcpp hello world xxx world and itcastxxxcpp
-
字符串的切割-split
my_str = "hello world itcast and itcastxxxcpp" # 生成一个列表,列表里的元素是my_str字符串以空格切割后的 print(my_str.split(" ")) # 运行结果 ['hello', 'world', 'itcast', 'and', 'itcastxxxcpp']
-
把字符串的第一个字符大写-capitalize
my_str = 'hello world' # 把字符串的第一个字符大写 print(my_str.capitalize()) # 运行结果 Hello world
-
把字符串的每个单词首字母大写-title
my_str = "hello world" #把字符串的每个单词首字母大写 print(my_str.title()) # 运行结果 Hello World
-
检测字符串是否以指定字符串开头,是True,否False-startswith
my_str = 'hello world' #检测字符串是否以hello开头 ,是True,否False print(my_str.startswith('hello')) #检测字符串是否以Hello开头 ,是True,否False print(my_str.startswith('Hello')) # 运行结果 True False
-
检测字符串是否以指定字符串结束,是True,否False-endswith
my_str = 'hello world' #检测字符串是否以world结束 ,是True,否False print(my_str.endswith('world')) #检测字符串是否以WORLD结束 ,是True,否False print(my_str.endswith('WORLD')) # 运行结果 True False
-
把字符串中所有大写字符转换成小写-lower
my_str = 'HELLO WORLD' #把字符串中大写字符转换成小写 print(my_str.lower()) # 运行结果 hello world
-
把字符串中所有小写字符转换成大写-upper
my_str = 'hello world' #把字符串中小写字符转换成大写 print(my_str.upper()) # 运行结果 HELLO WORLD
-
反回原字符串左对齐,并使用空格填充至你指定的长度的新字符串-ljust
my_str = 'hello world' # 反回原字符串左对齐,并使用空格填充至长度20的新字符串 print(my_str.ljust(20)) # 运行结果 hello world(后面是空格) hello world
-
反回原字符串右对齐,并使用空格填充至你指定的长度的新字符串-rjust
my_str = 'hello world' # 反回原字符串右对齐,并使用空格填充至长度20的新字符串 print(my_str.rjust(20)) # 运行结果 hello world(前面是空格) hello world
-
反回原字符串居中,并使用空格填充至你指定的长度的新字符串-center
my_str = 'hello world' # 反回原字符串居中,并使用空格填充至长度20的新字符串 print(my_str.center(20)) # 运行结果 hello world(前后是空格) hello world
-
删除左边的空白字符-lstrip
name = ' laowang' # 删除name左边的空白字符 print(name.lstrip()) # 运行结果 laowang
-
删除末尾的空白字符-rstrip
name = "laowang " # 删除name末尾的空白字符 print(name.rstrip()) # 运行结果 laowang
-
删除两端的空白字符-strip
name = " laowang " # 删除name端的空白字符 print(name.strip()) # 运行结果 laowang
-
以指定字符串分割成三部分,指定前的,指定的,指定后的-partition
my_str = "hello world itcast and itcastxxxcpp" # partition从左往右找 #把my_str以指定的字符串 itcast 分割成三部分,itcast前,itcast,itcast后 #从左开始查找然后分割成 元组元素 print(my_str.partition('itcast')) # 运行结果 ('hello world ', 'itcast', ' and itcastxxxcpp')
-
以指定字符串分割成三部分,指定前的,指定的,指定后的-rpartition
my_str = "hello world itcast and itcastxxxcpp" # rpartition从右往左找 #把my_str以指定的字符串 itcast 分割成三部分,itcast前,itcast,itcast后 #从右开始查找然后分割成 元组元素 print(my_str.rpartition('itcast')) # 运行结果 ('hello world itcast and ', 'itcast', 'xxxcpp')
-
按照行分隔,反回一个包含各行为元素的列表-splitlines
my_str = 'hello\nworld\nand\nitcast' # 按照\n分割,反回一个包含各行为元素的列表 print(my_str.splitlines()) # 运行结果 ['hello', 'world', 'and', 'itcast']
-
如果字符串里所有字符都是字母则反回True,否则False-isalpha
my_str = "helloworld" # 如果my_str所有的字符都是字母则反回True,否则反回False print(my_str.isalpha()) # 运行结果 True
-
如果字符串里所有字符都是数字则反回True,否则False-isdigit
my_str = "123456" # 如果my_str所有的字符都是数字则反回True,否则反回False print(my_str.isdigit()) # 运行结果 True
-
如果字符串里所有字符都是字母或数字则反回True,否则False-isalnum
my_str = "helloworld123456" # 如果my_str所有的字符都是字母或数字则反回True,否则反回False print(my_str.isalnum()) # 运行结果 True
-
字符串中每个字符后面插入个指定字符串,构造出新的字符串-join
my_str = ['my','name','is','dongGe'] my_string = 'hello world' # my_str中每个字符后面插入 空格 构造出新的字符串 print(' '.join(my_str)) print(' '.join(my_string)) # 运行结果 my name is dongGe h e l l o w o r l d
-
面试题
test = 'af \t nwo \t afge \t jas p ogo jeps jgm \t psjmgpes' # 把test里的的空格和\t删除掉 result = test.split() # split()里面什么都不写 默认是删除这些然后生成列表 print(''.join(result)) #把列表里面的字符串链接在一起 # 运行结果 afnwoafgejaspogojepsjgmpsjmgpes