python 字符串

# -*- coding: utf-8 -*-

#三引号可以用来多行注释

str1 = 'a'
str2 = "b"
str3 = '''b
cd
deff'''
print(str1)  #a
print(str2)  #b
print(str3)
#b
#cd
#deff

str3 = str1 + str2
str4 = str2*3 #bbb

str1 = 'hello hello wold' \
       'ni' \
    'hao'
print(str1)  #hello hello woldnihao   一行写不下可以用该方法

#字符通过切片拼接
str1 = str1[:3] + str1[2: 5] +str1[10:]
print(str1)  #hellloo woldnihao

#字符串比较大小
str1 = [1, 2, 3]
str2 = [4, 5, 'a']
print(str1 > str2)  #False

str1 = 'c:\new\table'
print(str1)
# c:
#ew    able

str1 = 'c:\\new\\table'
print(str1)
# c:\new\table

#字符串前加r以为这这段字符串为原始字符串,即保留原始本意,不做各种转意处理
str1 = r'c:\new\table'
print(str1)
# c:\new\table

#str2 = r'c:\new\table\'  #注意原始字符串尾部不能加\,否则会将报错,因为提前将引号转意了
#上面这个可以写成
str2 = r'c:\new\table' + '\\'
print(str2)  #c:\new\table\

#格式化format
str4 = 'hello {1} ni men {0} hao ma {2}'.format('a', 'b', 'c')
print(str4)  #hello b ni men a hao ma c
str4 = 'hello {a} ni men {c} hao ma {d}'.format(c = '1', a = '2', d = '3')
print(str4)  #hello 2 ni men 1 hao ma 3
str4 = 'hello {0} ni men {c} hao ma {d}'.format('1', d = '2', c = '3')
print(str4)  #hello 1 ni men 3 hao ma 2
#str4 = 'hello {a} ni men {c} hao ma {0}'.format(a = '1', c = '2', '3') #混用的时候数值的形式不能放在索引的后面,否则出错

str5 = 'hello %s ni men %d hao ma %f' % ('a', 11, 3.4)
print(str5)  #hello a ni men 11 hao ma 3.400000

#返回一个首字母大写的字符串capitalize()
str1 = 'hello world'
str2 = str1.capitalize()
print(str2)  #Hello world

#设置字符串长度, 设置居中center, 参数1宽度, 参数2是不足区域的补充字符,默认空
str3 = str1.center(50, '-')
print(str3) #-------------------hello world--------------------

#设置字符串长度,左对齐,长度不足用设置的字符填充,默认空字符
str1 = 'hello'
str2 = str1.ljust(10, '-')
print(str2) #hello-----

#设置字符串长度, 右对齐,长度不足用设置的字符填充,默认空字符
str1 = 'hello'
str2 = str1.rjust(10, '-')
print(str2) #-----hello

#设置字符串长度, 长度不足用0填充
str1 = 'hello'
str2 = str1.zfill(10)
print(str2) #00000hello

#查找某个子字符串出现的数量count
print(str1.count('ll'))  #1
print(str1.count('l', 0, 3)) #1   参数2为起始位置,  参数3为终止位置, 区间[0,3)

#是否以某字符串开头/结尾startswith/endswith
print(str1.startswith('he', 0, len(str1)-1)) #true
print(str1.endswith('rl', 0, len(str1)-1)) #true

#把字符串中的TAB换成空格,默认是一个tab等于8个空格
str1 = '    hello,      world'
str1 = str1.expandtabs(4)
print(str1) #    hello,      world

#查找子字符串并返回第一次找到的索引
str1 = 'hello,llo world!'
print(str1.find('ll')) #2
print(str1.find('ll', 3)) #6
print(str1.find('ll', 0, 6)) #2
print(str1.find('lll',)) #-1

#查找子字符串从右往左查并返回第一次找到的索引
str1 = 'hello,llo world!'
print(str1.rfind('ll')) #6
print(str1.rfind('ll', 3)) #6
print(str1.rfind('ll', 0, 6)) #2
print(str1.rfind('lll',)) #-1

#查找子字符串的索引,同上,找不到时有差异
print(str1.rindex('ll')) #6
print(str1.rindex('ll', 3)) #6
print(str1.rindex('ll', 0, 6)) #2
#print(str1.index('lll',)) #ValueError: substring not found

#查找子字符串的索引,同上,找不到时有差异
print(str1.index('ll')) #2
print(str1.index('ll', 3)) #6
print(str1.index('ll', 0, 6)) #2
#print(str1.index('lll',)) #ValueError: substring not found

#查找在不在字符串中
print('llo' in str1)  #True
print('llog' not in str1)  #True

#是否全是字母和数字,并至少有一个字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isalnum())  #False
print(str2.isalnum())  #False
print(str3.isalnum())  #True
print(str4.isalnum())  #True
print(str5.isalnum())  #True
print(str6.isalnum())  #False

#是否全是字母并至少有1个字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isalpha())  #False
print(str2.isalpha())  #False
print(str3.isalpha())  #False
print(str4.isalpha())  #True
print(str5.isalpha())  #False
print(str6.isalpha())  #False

#是否全是字母并至少有1个字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isdigit())  #False
print(str2.isdigit())  #False
print(str3.isdigit())  #True
print(str4.isdigit())  #False
print(str5.isdigit())  #False
print(str6.isdigit())  #False

#是否全是空白符(空格/回车/换行/TAB),并至少有1个字符
str1 = 'hello world'
str2 = '    '#4个空格
str3 = '    '#1个tab
str4 = ''
str5 = '\n\r'
print(str1.isspace())  #False
print(str2.isspace())  #True
print(str3.isspace())  #True
print(str4.isspace())  #False
print(str5.isspace())  #True 回车换行符也算空白符

#是否全是小写字母
str1 = 'hello world'
str2 = 'HELLO World1'
str3 = '1231244'
str4 = '@#$%^&'
str5 = '@#$%^&hello'
str6 = ''
print(str1.islower())  #True
print(str2.islower())  #False
print(str3.islower())  #False
print(str4.islower())  #False
print(str5.islower())  #True
print(str6.islower())  #False

#是否全是大写字母
str1 = 'hello world'
str2 = 'HELLO WORLD'
str3 = '1231244'
str4 = '@#$%^&HELLO'
str5 = '@#$%^&'
str6 = ''
print(str1.isupper())  #False
print(str2.isupper())  #True
print(str3.isupper())  #False
print(str4.isupper())  #True
print(str5.isupper())  #False
print(str6.isupper())  #False

#是否全标题样式(每个单词首字母大写)
str1 = 'Hello World'
str2 = 'Hello world'
str3 = '1231244'
str4 = '@#$%^&HELLO'
str5 = '@#$%^&'
str6 = ''
print(str1.istitle())  #True
print(str2.istitle())  #False
print(str3.istitle())  #False
print(str4.istitle())  #False
print(str5.istitle())  #False
print(str6.istitle())  #False

#S.join(seq) #把seq字符串序列,用S连接起来
str1 = '123'
str2 = str1.join('abcd')
print(str2)  #a123b123c123d

#转化为小写
str1 = 'HeLLo WoRld'
str2 = str1.lower()
print(str2)  #hello world

#转化为大写
str1 = 'HeLLo WoRld'
str2 = str1.upper()
print(str2)  #HELLO WORLD

#转化为标题样式
str1 = 'HeLLo WoRld'
str2 = str1.title()
print(str2)  #Hello World

#大写变小写, 小写变大写
str1 = 'HeLLo WoRld'
str2 = str1.swapcase()
print(str2)  #hEllO wOrLD

#删除左侧空白字符
str1 = ' \n \r   HeLLo WoRld' #左侧一个空格,一个TAB
str2 = str1.lstrip()
print(str2)  #HeLLo WoRld

#删除右侧空白字符
str1 = 'HeLLo WoRld \n \r   ' #左侧一个空格,一个TAB
str2 = str1.rstrip()
print(str2)  #HeLLo WoRld

#删除两侧空白字符
str1 = '\n HeLLo WoRld \n \r   ' #左侧一个\n一个空格,右侧一个空格,\n空格\r一个TAB
str2 = str1.strip()
print(str2)  #HeLLo WoRld

#S.partition(seq) 从S中找seq, 从左往右找到第一个后,咋把seq前面的部分,seq, 和seq后面的部分三个组成一个元组返回
str1 = 'hello world'
tuple1 = str1.partition('or')
print(tuple1)  #('hello w', 'or', 'ld')
tuple2 = str1.partition('l')
print(tuple2)  #('he', 'l', 'lo world')

#S.partition(seq) 从S中找seq, 从右往左找到第一个后,咋把seq前面的部分,seq, 和seq后面的部分三个组成一个元组返回
str1 = 'hello world'
tuple1 = str1.rpartition('or')
print(tuple1)  #('hello w', 'or', 'ld')
tuple2 = str1.rpartition('l')
print(tuple2)  #('hello wor', 'l', 'd')

#替换字符串中的子字符串
str1 = 'hello world lol'
str2 = str1.replace('o', 'm')
print(str2)  #hellm wmrld lml
str3 = str1.replace('o', 'm', 2) #第三个参数为替换不超过多少次
print(str3)  #hellm wmrld lol

#S.split(seq, num) 把字符串S以seq从左到右进行切片,不超过num个
str1 = 'helloworldlol'
list1 = str1.split('o')
print(list1)  #['hell', 'w', 'rldl', 'l']
list2 = str1.split('o', 2)
print(list2)  #['hell', 'w', 'rldlol']

#S.split(seq, num) 把字符串S以seq从右到左进行切片,不超过num个
str1 = 'helloworldlol'
list1 = str1.rsplit('o')
print(list1)  #['hell', 'w', 'rldl', 'l']
list2 = str1.rsplit('o', 2)
print(list2)  #['hellow', 'rldl', 'l']

#S.splitlines() 把字符串S行的形式进行切片,相当于去除尾部1个\n,然后根据\n进行切片
str1 = 'hello\nworld\nlol'
list1 = str1.splitlines()
print(list1)  #['hello', 'world', 'lol']
str2 = ' \nhello\nworld\nlol\n '
list2 = str2.splitlines()
print(list2)  #[' ', 'hello', 'world', 'lol', ' ']
str3 = '\n\n\nhello\nworld\nlol\n\n\n'
list3 = str3.splitlines()
print(list3)  #['', '', '', 'hello', 'world', 'lol', '', '']

#相当于根据映射关系maketrans将字符串中的相关字符替换掉
str1 = 'eod'
str2 = '123' #str2长度必须和str1相同,不然maketrans报错 ValueError: maketrans arguments must have same length
from string import maketrans
str3 = 'hello world lol'
str4 = str3.translate(maketrans(str1, str2))
print(str4)  #h1ll2 w2rl3 l2l
str5 = str3.translate(maketrans(str1, str2), 'hr') #删除h和r字符
print(str5)  #1ll2 w2l3 l2l



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章